PowerShell脚本块远程调用第三方应用VizientSC.exe的实现问题
Got it, let's work through how to run that VizientSC.exe command remotely via a PowerShell ScriptBlock. I’ve dealt with similar third-party app remote execution scenarios before, so here’s what you need to know:
Prerequisites
First, make sure these boxes are checked to avoid basic roadblocks:
- WinRM is configured on the target machine: You’ll need to enable PowerShell remoting if it’s not already set up. On the remote machine, run
Enable-PSRemoting -Forceas an administrator. - VizientSC.exe exists at the exact path on the remote machine: Double-check that the path
C:\Program Files (x86)\Vizient\Vizient Secure Channel v2.1\VizientSC.exeis identical on the target system—third-party apps sometimes install to different locations depending on OS architecture or user context. - Your account has remote permissions: Ensure your user is part of the
Remote Management Usersgroup on the target machine, or has administrative rights to execute commands remotely.
Basic Implementation
Here’s a straightforward script that wraps your existing command in a ScriptBlock and runs it remotely with Invoke-Command:
# Define your target remote computer (use hostname, IP, or FQDN) $remoteComputer = "YourTargetPCName" # Get credentials for the remote machine (this will prompt you for a username/password) $remoteCreds = Get-Credential # Execute the command remotely Invoke-Command -ComputerName $remoteComputer -Credential $remoteCreds -ScriptBlock { # Use the call operator (&) to execute the external EXE & "C:\Program Files (x86)\Vizient\Vizient Secure Channel v2.1\VizientSC.exe" ` UID=me@musc.edu ` PWD=XXXXXXXXX ` HCOID=123456 ` PRODTYPE=PRO-UHCSECURECHANNEL-CDB ` PACKAGETYPE=OTH ` FOLDERPATH="\..." }
Key Notes:
- The
&operator is critical here—it tells PowerShell to execute the external executable file (since the path has spaces, we wrap it in quotes). - The backtick (`) at the end of each line is just for readability; you can run the entire command on one line if you prefer.
Secure Version (Avoid Plaintext Passwords)
Hardcoding passwords in scripts is a big security no-no. Here’s how to pass credentials securely without exposing plaintext:
$remoteComputer = "YourTargetPCName" $localUsername = "me@musc.edu" # Convert your password to a SecureString (you can also store this securely in a file) $securePassword = ConvertTo-SecureString "YourActualPassword" -AsPlainText -Force $remoteCreds = New-Object System.Management.Automation.PSCredential ($localUsername, $securePassword) Invoke-Command -ComputerName $remoteComputer -Credential $remoteCreds -ScriptBlock { # Use the $using: keyword to access local variables in the remote ScriptBlock $decryptedPassword = $using:securePassword | ConvertFrom-SecureString -AsPlainText & "C:\Program Files (x86)\Vizient\Vizient Secure Channel v2.1\VizientSC.exe" ` UID=$using:localUsername ` PWD=$decryptedPassword ` HCOID=123456 ` PRODTYPE=PRO-UHCSECURECHANNEL-CDB ` PACKAGETYPE=OTH ` FOLDERPATH="\..." }
For even better security, you can save the SecureString to an encrypted file instead of defining it in the script (use ConvertTo-SecureString | Out-File "C:\Path\To\SecurePassword.txt" and read it back with Get-Content "C:\Path\To\SecurePassword.txt" | ConvertTo-SecureString).
Troubleshooting Tips
If you run into issues, check these common pain points:
- "The term '...VizientSC.exe' is not recognized": Verify the path on the remote machine—sometimes 64-bit PowerShell will look in
Program Filesinstead ofProgram Files (x86), so try using$env:ProgramFiles(x86)to dynamically get the path:& "$($env:ProgramFiles(x86))\Vizient\Vizient Secure Channel v2.1\VizientSC.exe" ... - Permission denied: Confirm your account has rights to run the EXE on the remote machine, and that WinRM allows your user to connect. You can test basic connectivity with
Test-WSMan $remoteComputer. - No output or unexpected behavior: Capture the EXE’s output and errors to debug:
The$output = & "C:\Path\To\VizientSC.exe" ... 2>&1 return $output2>&1redirects error output to the same stream as standard output, so you’ll see any issues the EXE throws.
内容的提问来源于stack exchange,提问作者Peter Plott

