2017-08-22 68 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 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    } 
}