2017-08-22 115 views
0

我正在運行以下腳本,出於某種原因,我編輯了這個並將其打破。它從另外兩個腳本一起修補。我得到的錯誤是:50行自動應用程序部署解決方案

Remove-PSSession:無法將參數綁定到參數'Session',因爲它是空的。

出於某種原因,它不是從文本文件/ CSV中提取計算機名稱,我在那裏有一個計算機名稱作爲演示/測試。
有誰知道什麼可能是錯的?

這是腳本:

Get-ADComputer -filter {Enabled -eq $True} -Properties cn -SearchBase "OU=Tablets,OU=DEPT,OU=Computer Accounts,DC=BUSINESS,DC=LOCAL" | select cn | Out-File c:\tablets.txt 

$cred = Get-Credential BUSINESS\XY.admin 
$computers = gc "C:\tablets.csv" 
$TargetSession = $computers 

# This is the directory you want to copy to the computer (IE. c:\folder_to_be_copied) 
$source = "c:\apps" 

# On the destination computer, where do you want the folder to be copied?   
$dest = "c$" 

foreach ($computer in $computers) { 
    { 
     Copy-Item $source -Destination \\$computer\$dest -Recurse 
    } 
} 

foreach ($computer in $TargetSession) { 
    { 
     #Creates a new remote PowerShell Session and script block - enter the code you want to execute remotely from this block 
     $Session = New-PSSession $computer -Credential $cred 
     Invoke-Command -Session $Session -ScriptBlock { 
      Start-Process "C:\apps\admin-deploy.msi" -ArgumentList "/qn" -Wait 
      #Start-Sleep -s 20; 
      #Start-Process "" 
     } 
    } 
} 
# Clean Up 
Remove-PSSession -Session $Session 
+0

如果它是很難讀 – arco444

+1

因爲你的循環有太多的'{}'請格式化你的代碼,努力幫助你。每個循環只是生成一個腳本塊到管道,它不會運行任何東西。比較'foreach($ i in 1..5){$ i}'和'foreach($ i in 1..5){{$ i}}''。並且你的remove-pssession應該在invoke-command完成後的創建循環中。 – TessellatingHeckler

+0

這些無意義的嵌套scriptblocks是什麼?這是一種新的時尚嗎? –

回答

0

您所創建的文件的名稱是c:\tablets.txt但要導入c:\tablets.csv因此,除非你改變$computers以下它將是空的:

$computers = gc "C:\tablets.txt" 
0

Remove-PSSession置於循環內部以分別處理每個會話。

foreach ($computer in $TargetSession) 
{ 
    { 
     #Creates a new remote PowerShell Session and script block - enter the code you want to execute remotely from this block 
     $Session = New-PSSession $computer -Credential $cred 
     Invoke-Command -Session $Session -ScriptBlock { 
      Start-Process 「C:\apps\admin-deploy.msi」 -ArgumentList 「/qn」 -Wait 
      #Start-Sleep -s 20; 
      #Start-Process "" 
     } 
     Remove-PSSession -Session $Session # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    } 
}