错误报告 - 检查和删除文件,注册表和环境变量

时间:2018-05-25 16:39:39

标签: powershell

我仍然是脚本编写的新手,所以这可能比它需要的更笨重,或者完全错误。使用PowerShell我正在尝试删除一个程序,并通过删除许多服务器中的文件,注册表项和环境变量来清除它。我的脚本我管道发球和一个错误记录文件。我遇到的问题是它无法正常工作,但我没有为我排除故障。当我直接运行或开球时。我在这里做错了什么想法?

$servers = import-csv -Path .\ServerList.txt
foreach ($server in $Servers) {
    Write-host "Copying .bat file to $server at F:"
    Copy-item -path \\e-fake\d-fake\o-fake\AdminScripts\PatrolRemove\PtrAgentRemove.bat -Destination f:\PtrAgentRemove.bat
    start-sleep -Seconds 2
    F:\PtrAgentRemove.bat
    start-sleep -Seconds 5


    $regkeypath = "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\BMC Software"
    $regValue = (Get-ItemProperty $regkeypath).test -eq $null
    If ($regValue -eq $false) {
        Remove-Item "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\BMC Software"
    } 
    Else {
        Write-Host "The BMC Software registry entry does not exist, skipping the registry removal step"
    }

    If ($regValue -eq $false) {
        Write-Host "Registry entry Succefully removed"
    } 
    Else {
        Write-Host "Failed to remove BMC Software registry entry"
    }

    Write-host "Removing Enviromental Variables"
    $ENVPHome = get-childitem Env:\PATROL_HOME
    $ENVPTemp = Get-ChildItem Env:\PATROL_Temp
    $envPHtest = Test-Path Env:\PATROL_HOME
    $envPTtest = Test-Path Env:\PATROL_Temp
    If ($envPHtest -eq $true) {
        Remove-item Env:\PATROL_HOME
    } 
    Else {
        Write-host " Env:\PATROL_Home does not exist on $server"
    }

    If ($envPTtest -eq $true) {
        Remove-item Env:\PATROL_TEMP
    } 
    Else {
        Write-host " Env:\PATROL_Temp does not exist on $server"
    }

    Write-host "Testing Enviromrntal Variables"
    $envPHtest = Test-Path Env:\PATROL_HOME
    $envPTtest = Test-Path Env:\PATROL_Temp
    If ($envPHtest -eq $false) {
        Write-host " Env:\PATROL_Home does is not on $server"
    } 
    Else {
        Write-host " Removal of Env:\PATROL_Home failed $server"
    }

    If ($envPTtest -eq $false) {
        Write-host " Env:\PATROL_TEMP does is not on $server"
    } 
    Else {
        Write-host " Removal of Env:\PATROL_TEMP failed $server"
    }

    Write-Host "Removing F:Patrol directory"
    $PatrolFDIR = Get-childitem F:\Patrol
    $PatrolCDIR = Get-Childitem C:\users\Patrol8
    $PatrolUDIR = Get-Childitem U:\users\Patrol8
    $PatrolFDIRTest = Test-path F:\Patrol
    $PatrolCDIRTest = Test-path c:\users\Patrol8
    $PatrolUDIRTest = Test-path U:\users\Patrol8

    If ($PatrolFDIRTest -eq $true) {
        Remove-item $PatrolFDIR
    } 
    Else {
        Write-host " F:\Patrol does not exist on $server"
    }

    If ($PatrolCDIRTest -eq $true) {
        Remove-item $PatrolCDIR
    } 
    Else {
        Write-host " C:\users\Patrol8 does not exist on $server"
    }
    If ($PatrolUDIRTest -eq $true) {
        Remove-item $PatrolUDIR
    } 
    Else {
        Write-host " U:\users\Patrol8 does not exist on $server"
    }

}        
Write-host "Script succussfully ran, please check the log file for errors"

0 个答案:

没有答案