我已經編寫了一個腳本,將.EXE跨域推送到多個遠程服務器並進行安裝。當我執行腳本時,它會嘗試在本地機器上運行安裝程序。對我的腳本的任何審查和更正都會有很大的幫助。謝謝PowerShell遠程無法正常工作
$uname='domain\username'
$pwd='Password' | ConvertTo-SecureString -Force -AsPlainText
try {
$targets = Get-Content -Path D:\scripts\serverlist.txt
$source = C:\Downloads\installer.exe
$creds=New-object System.Management.Automation.PSCredential ($uname, $pwd)
[ScriptBlock] $code = {
if(!(Test-Path "C:\Temp"))
{
New-Item -Path "C:\Temp" -ItemType Directory
}
}
function Installer([string] $file, [String] $argslist)
{
$ErrorActionPreference="Stop"
try
{
$exec = Start-Process -FilePath $file -ArgumentList $argslist -Wait -PassThru
Write-Host "Exit code for installation of $file is $($exec.ExitCode)"
return $exec.ExitCode
}
catch
{
throw
}
}
foreach($target in $targets){
$name = GC env:computername
$current = New-PSSession -ComputerName $name -Credential $creds -ErrorAction SilentlyContinue
Invoke-Command -Session $current -ScriptBlock $code
Robocopy $source E:\Temp
Installer "\\$name\$source" "/q /norestart"
$current | Remove-PSSession
} }
catch
{
Write-Host -f red "An error has occurred!"
Write-Host "$($error[0].exception)"
$error.clear()
exit $LASTEXITCODE
}
啓動過程將始終在本地計算機上運行。您可能需要Invoke-Command –
謝謝Sami。但是該函數從遠程服務器中被調用。所以Start-Process會在本地觸發它嗎?但是,儘管如此,我試圖用Invoke-Command替換它,但仍然無法使用。我得到下面的錯誤: – Karthik
您創建一個會話,但您不輸入或在那裏調用該函數,因此它在本地調用 –