我正在运行以下脚本,出于某种原因,我编辑了这个并将其打破。它从另外两个脚本一起修补。我得到的错误是: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
如果它是很难读 – arco444
因为你的循环有太多的'{}'请格式化你的代码,努力帮助你。每个循环只是生成一个脚本块到管道,它不会运行任何东西。比较'foreach($ i in 1..5){$ i}'和'foreach($ i in 1..5){{$ i}}''。并且你的remove-pssession应该在invoke-command完成后的创建循环中。 – TessellatingHeckler
这些无意义的嵌套scriptblocks是什么?这是一种新的时尚吗? –