首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PowerShell传递凭据以在远程计算机上运行批处理脚本

使用PowerShell传递凭据以在远程计算机上运行批处理脚本
EN

Stack Overflow用户
提问于 2018-07-23 18:45:49
回答 1查看 3.4K关注 0票数 0

我一直在编写一个脚本,它将循环遍历计算机列表,如果存在旧AV软件,则卸载旧AV软件;如果不存在,则安装我们的新AV软件。脚本通过执行两个文件来做到这一点:一个是一个批处理脚本(称为Sphos-Installation.bat),它将检查旧AV软件的存在,并运行我们新的AV供应商的竞争对手删除工具。一旦完成,批处理脚本将静默地启动可执行文件来安装我们的新AV软件。另一个文件将只删除旧的AV软件,因为我的环境中有一些计算机两者都有。下面是脚本:

代码语言:javascript
复制
#Variables that store the path to 32-bit and 64-bit versions of Sophos
$Sophos64 = Test-Path 'C:\Program Files\Sophos'
$Sophos32 = Test-Path 'C:\Program Files (x86)\Sophos'

#Variables that store the path to 32-bit and 64-bit versions of Kaspersky
$Kaspersky64 = Test-Path 'C:\Program Files\Kaspersky Lab'
$Kaspersky32 = Test-Path 'C:\Program Files (x86)\Kaspersky Lab'

#The following block will run the Sophos-Installation batch file, removing any instance of Kaspersky and installing Sophos
      If ($Sophos64 -eq $false -or $Sophos32 -eq $false) 
      {
      Start-Process -FilePath C:\Temp\AVInstall\Sophos-Installation.bat
      $env:COMPUTERNAME |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Sophos_Installed.txt" -Append
      }
#The following block will remove Kaspersky from a machine if it is present and Sophos is already installed.
        Elseif (($Kaspersky64 -eq $true -or $Kaspersky32 -eq $true) -and ($Sophos64 -eq $true -or $Sophos32 -eq $true)) 
        {
        Start-Process -FilePath C:\Temp\AVInstall\AVRemoveW.exe
        $env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Kaspersky_Removed.txt" -Append
        }
#The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
        else {$env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Proper_Configuration.txt" -Append}

在启动上述脚本并将计算机名记录到我的PC之前,我已经运行了另一个脚本,该脚本将必要的文件复制到远程计算机上。(现在没有成功。当脚本试图写入.txt文件时,我会得到一个拒绝访问的错误。我现在不太关心这个问题,但如果有人想在这方面做点什么的话,那就自由点吧。)下面是这个脚本:

代码语言:javascript
复制
    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
ForEach ($computer in $computers)
{
$connection = Test-Connection -ComputerName $computer -Quiet
if ($connection = $true)
{
try{
Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force
Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1'
echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
}
catch {
echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
}
}
else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
}

当运行此脚本时,文件复制正确,批处理文件执行(批处理文件在运行时还会在远程机器上创建日志,因此我知道它正在执行),但旧AV没有卸载,新AV也没有安装。当我远程进入我的一台测试计算机时,我手动运行了S磷-Installation.bat文件,并得到了以下错误:

访问被拒绝。访问被拒绝。用户名或密码不正确。

然后提示我输入我的管理凭据,然后批处理文件按预期工作。所以很明显,我需要将我的管理凭证传递给这些机器,这样才能让它正常工作。每次我试图在PowerShell中传递凭据时,它都没有正常工作,所以我希望这里有人能告诉我以下几点:

  • 如何设置要工作的凭据变量
  • 在上面的脚本中,我需要实现这个凭据变量。
  • 任何其他的信息,我可能需要知道,以使这个工作正常。

请记住,当我回答说我是PowerShell的初学者(总的来说,这是我的第一份it工作)时,我大约6周前上了一堂课,这是我第一个实现它的项目。提前感谢您的帮助!

更新:我在脚本中添加了一个凭据变量。我已经在三个地方实现了它,并且我仍然会收到提示,在我的终端输入凭证。以下是我更新的脚本:

代码语言:javascript
复制
#Variables that store the path to 32-bit and 64-bit versions of Sophos
$Sophos64 = Test-Path 'C:\Program Files\Sophos'
$Sophos32 = Test-Path 'C:\Program Files (x86)\Sophos'

#Variables that store the path to 32-bit and 64-bit versions of Kaspersky
$Kaspersky64 = Test-Path 'C:\Program Files\Kaspersky Lab'
$Kaspersky32 = Test-Path 'C:\Program Files (x86)\Kaspersky Lab'

#The following block will run the CCOB-Sophos-Installation batch file, removing any instance of Kaspersky and installing Sophos
      If ($Sophos64 -eq $false -or $Sophos32 -eq $false) 
      {
      Start-Process -FilePath C:\Temp\AVInstall\CCOB-Sophos-Installation.bat -Credential $cred 
      $env:COMPUTERNAME |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Sophos_Installed.txt" -Append
      }
#The following block will remove Kaspersky from a machine if it is present and Sophos is already installed.
        Elseif (($Kaspersky64 -eq $true -or $Kaspersky32 -eq $true) -and ($Sophos64 -eq $true -or $Sophos32 -eq $true)) 
        {
        Start-Process -FilePath C:\Temp\AVInstall\AVRemoveW.exe -Credential $cred
        $env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Kaspersky_Removed.txt" -Append
        }
#The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
        else {$env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Proper_Configuration.txt" -Append}

以及:

代码语言:javascript
复制
    #credentials
$username = "mydomain\myusername"
$password = Get-Content -Path 'C:\Sophos Results\mysecurestring.txt' | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
    ForEach ($computer in $computers)
    {
    $connection = Test-Connection -ComputerName $computer -Quiet
    if ($connection -eq $true)
    {
    try{
    Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force 
    Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1' -Credential $cred
    echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
    }
    catch {
    echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
    }
    }
    else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
    }

虽然创建安全字符串文件的命令不在脚本中,但我确实创建了它。我已经在调用命令和两个启动进程命令中使用了凭据参数,但是仍然提示我输入用户名和密码。

EN

回答 1

Stack Overflow用户

发布于 2018-08-03 03:53:18

请您测试一下下面的代码,我已经添加了一份证书

代码语言:javascript
复制
$Username = "administrator"
 $Password  = ConvertTo-SecureString 'Administrator password here' -AsPlainText -Force

     $Credentials  = New-Object System.Management.Automation.PSCredential $Username, $Password

    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
    ForEach ($computer in $computers)
    {
    $connection = Test-Connection -ComputerName $computer -Quiet
    if ($connection = $true)
    {
    try{
    Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force -Credential $Credentials
    Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1' -Credential $Credentials
    echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
    }
    catch {
    echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
    }
    }
    else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51485377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档