50线自动化应用部署解决方案

时间:2017-08-22 07:58:54

标签: powershell windows-applications

我正在运行以下脚本,出于某种原因,我已对此进行了编辑并将其破坏。它是从其他2个脚本拼凑而成的。我得到的错误是:

  

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

2 个答案:

答案 0 :(得分:0)

您创建的文件的名称为c:\tablets.txt,但您要导入c:\tablets.csv,因此除非您将$computers更改为以下内容,否则它将为空:

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

答案 1 :(得分: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 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }
}