首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过文件路径搜索软件,然后使用PowerShell安装丢失的软件

如何通过文件路径搜索软件,然后使用PowerShell安装丢失的软件
EN

Stack Overflow用户
提问于 2018-07-17 23:58:20
回答 1查看 89关注 0票数 0

我正在尝试编写一个PowerShell脚本,使我能够在我的域中的每台计算机上搜索旧的防病毒程序,如果它有该程序,请运行我们的新antivirus软件供应商提供给我们的竞争对手删除工具。我还希望脚本搜索我们的新反病毒软件,如果它丢失了,就安装它。竞争对手删除工具和安装程序的.exe都存储在域控制器上,并通过网络共享进行访问。我正在我的组织中启用PowerShell远程处理,我希望这会给我带来更大的灵活性。这是我到目前为止所知道的:

代码语言:javascript
复制
   #Retrieves list of all computers tied to Active Directory
$computers = Get-Content -Path 'C:\Sophos Results\ADComputers.txt'

#Paths to Sophos Installation and the Sophos Competitor Removal Tool
$SophosInstallPath =  Get-ChildItem -File \\DC-02\netlogon\SophosInstall\Sophos-Installation.bat
$KasperskyRemove = Get-ChildItem -File \\DC-02\netlogon\SophosInstall\AVRemoveW.exe


#Loops through each AD Computer and tests for proper software installation 
ForEach ($computer in $computers)
   {
#Variables that store the path to 32-bit and 64-bit versions of Sophos
$Sophos64 = Test-Path "\\$computer\c$\Program Files\Sophos"
$Sophos32 = Test-Path "\\$computer\c$\Program Files (x86)\Sophos"

#Variables that store the path to 32-bit and 64-bit versions of Kaspersky
$Kaspersky64 = Test-Path "\\$computer\c$\Program Files\Kaspersky Lab"
$Kaspersky32 = Test-Path "\\$computer\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 $SophosInstallPath -Verbose
      Write-Host "Beginning Sophos Installation on $computer"
      }
#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 $KasperskyRemove -Verbose
        Write-Host "Removing Kaspersky from $computer"
        }
#The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
        else {Write-Host "$computer has the proper AV software installation"}
   }

目前,当我在一组测试计算机上运行此程序时,我没有得到任何错误,我知道脚本正确地确定是否安装了Kaspersky/Sophos,但它不会启动竞争对手删除工具或安装文件。我知道一旦启用了PowerShell remoting,就会有更好的方法来做这件事,我只是不确定那是什么方法。有什么建议吗?提前感谢

更新:

我在本地计算机上启用了PS Remoting,并尝试了以下操作:

代码语言:javascript
复制
Invoke-Command -ComputerName localhost -ScriptBlock { *Script from above* }

我得到了以下错误:

代码语言:javascript
复制
Access is denied
    + CategoryInfo          : PermissionDenied: (\\dc-02\net...nstallation.bat:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost   Cannot find path '\\dc-02\netlogon\SophosInstall\Sophos-Installation.bat' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\dc-02\net...nstallation.bat:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost   Access is denied
    + CategoryInfo          : PermissionDenied: (\\dc-02\net...l\AVRemoveW.exe:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost   Cannot find path '\\dc-02\netlogon\SophosInstall\AVRemoveW.exe' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\dc-02\net...l\AVRemoveW.exe:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost   Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command  again.
    + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName        : localhost

我从我的域管理员帐户运行PowerShell,所以我不确定为什么它会显示访问被拒绝。谢谢你的帮助,我对IT和PowerShell都是个新手。我的第一份工作两个月了

我的最后一个问题:当我准备好在更大的规模上做这件事时,假设我的测试组有3台计算机(1台使用Kaspersky,1台使用Sophos,1台都没有),下面应该可以工作,对吗?

代码语言:javascript
复制
 $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'

Invoke-Command -ComputerName $computers -ScriptBlock { *Original Script here* }

在处理多台计算机时,是否优先选择Invoke-Command和Enter-PSSession?在Invoke-Command的描述中写道:“要在远程计算机上运行单个命令,请使用ComputerName参数。要运行一系列共享数据的相关命令,请使用New-PSSession cmdlet”。因此,如果我确实使用了Invoke-Command (对我来说似乎更直接),我是否应该保存脚本,然后在scriptblock中调用它?然后,这让我想知道如何从远程计算机上的本地计算机调用脚本?我为这么多问题道歉,我越往下钻,我的头就越转。

更新7/19/2018:

又遇到了一个问题,希望这是最后一个。我将文件从远程服务器复制到我的本地机器上,以摆脱第二跳问题。在脚本中,我只是将文件从我的本地计算机复制到所有远程计算机。问题是,当我从本地复制到远程时,它只有在指定计算机名称时才起作用,如下所示:

代码语言:javascript
复制
Copy-Item -Path C:\Temp\AVInstall -Destination '\\Computer1\c$\Temp\AVInstall' -Recurse

如果我尝试使用计算机变量,如下所示,我得到一个网络路径未找到错误:

代码语言:javascript
复制
Copy-Item -Path C:\Temp\AVInstall -Destination '\\$computer\c$\Temp\AVInstall' -Recurse

为什么会这样呢?

EN

回答 1

Stack Overflow用户

发布于 2018-07-18 01:17:48

正如EBGreen提到的,您当前正在本地计算机上运行此程序。

您需要通过交互式PSSession或通过调用脚本在远程机器上运行它。

要使用Invoke-Command,您可以将要在远程机器上执行的脚本放入scriptblock中。然后在字符串数组中的每台机器上通过-computername参数进行调用。

我无法对此进行测试,但我认为您在尝试检索$SophosInstallPath$KasperskyRemove项时可能会遇到double-hop problem。如果确实发生了这种情况,它很可能会表现为“访问被拒绝”。如果发生这种情况,最简单的解决方案很可能是首先通过copy-item或更健壮的robocopy.exe将项目复制到机器上,然后在完成后删除它们。

代码语言:javascript
复制
#Retrieves list of all computers tied to Active Directory
$computers = Get-Content -Path 'C:\Sophos Results\ADComputers.txt'

$scriptblock = {
    #Paths to Sophos Installation and the Sophos Competitor Removal Tool
    $SophosInstallPath = Get-Item -File "\\DC-02\netlogon\SophosInstall\Sophos-Installation.bat"
    $KasperskyRemove = Get-Item -File "\\DC-02\netlogon\SophosInstall\AVRemoveW.exe"

    #Variables that store the path to 32-bit and 64-bit versions of Sophos
    $Sophos64 = Test-Path "\Program Files\Sophos"
    $Sophos32 = Test-Path "\Program Files (x86)\Sophos"

    #Variables that store the path to 32-bit and 64-bit versions of Kaspersky
    $Kaspersky64 = Test-Path "\Program Files\Kaspersky Lab"
    $Kaspersky32 = Test-Path "\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 $SophosInstallPath -Verbose
        Write-Host "Beginning Sophos Installation on $computer"
    }
    #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 $KasperskyRemove -Verbose
        Write-Host "Removing Kaspersky from $env:computer"
    }
    #The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
    else 
    {
        Write-Host "$env:computer has the proper AV software installation"
    }
}

Invoke-Command -ComputerName $computers -ScriptBlock $scriptblock
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51385762

复制
相关文章

相似问题

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