Powershell工作流程:使用if / else语句?

时间:2016-11-17 18:15:12

标签: powershell

我正在尝试使用工作流程,以便通过在多台计算机上并行运行ping来使脚本运行得更快。我遇到困难的地方是尝试在工作流程中使用我的if / else语句,并根据ping的结果添加日志条目。现在,它没有根据ping结果向日志写任何内容。任何帮助将不胜感激。我愿意接受不同方法的建议,只要它们不涉及使用模块。我想坚持使用Powershell代码(我没有权限在运行它的服务器上安装模块)。谢谢!这是我目前的代码:

<#  
.NOTES
===========================================================================
 Created on:    10/6/2016 8:06 AM
 Created by:    
 Organization:  
 Filename:      Get-MPOSOfflinePrinters.ps1
===========================================================================
.DESCRIPTION
#> 

$logfile = "D:\Logs\MPOSPrinterPingLog.txt"
$offlineprinters = "D:\Reports\MPOS\MPOSOfflinePrinters.txt"
If (Test-Path $logfile) {Remove-Item $logfile}
If (Test-Path $offlineprinters) {Remove-Item $offlineprinters}

Add-Content $logfile "Setting local path"
$localPath = "C:\Temp\MPOS"

Add-Content $logfile "Gathering server list"
$serverList = (Get-ADComputer -Filter "Name -like 'Q0*P30' -or Name -like 'Q0*P32'" -SearchBase "OU=Print,OU=Prod,OU=POS,DC=COMPANY,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSPrintServers.txt
$serverListPath = "C:\Temp\MPOS\MPOSPrintServers.txt"

#Retrieve a list of MPOS Print servers from text file and set to $serverNames
Add-Content $logfile "Compiling text file"
$serverNames = Get-Content -Path $serverListPath

#Iterate through each of the server names
foreach ($serverName in $serverNames) {

Add-Content $logfile "Processing $serverName" 

    #Check if the server is online before doing the remote command
    If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

        Add-Content $logfile "$serverName is online"

        #copy config file from MPOS print to local server for processing
        $timestamp1 = (Get-Date -Format g)
        Add-Content $logfile "$timestamp1 - Copying xml file from server to local path"
        $configPath = "\\$($serverName)\C$\ProgramData\Microsoft\Point Of Service\Configuration\Configuration.xml"
        Copy-Item $configPath $localPath 

        #process xml file to parse IP addresses for ping
        $timestamp2 = (Get-Date -Format g)
        Add-Content $logfile "$timestamp2 - Processing xml file from $serverName to parse data to csv"
        $xml = [xml](Get-Content C:\Temp\MPOS\Configuration.xml)
        $PrinterNames = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{LogicalName=$_.LogicalName.Name}}
        $PrinterIPs = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{HardwarePath=$_.HardwarePath}} 

        workflow Test-WFConnection {

            param(

            [string[]]$PrinterIPs

            )

            foreach -parallel ($PrinterIP in $PrinterIPs) {

                $pingableIP = $PrinterIP.HardwarePath

                If (Test-Connection -ComputerName $pingableIP -Quiet -Count 1 -ErrorAction SilentlyContinue) {

                    $timestamp3 = (Get-Date -Format g)
                    Add-Content -Path $logfile -Value ("$timestamp3 - $serverName, $pingableIP is online and pingable")

                } #If (Test-Connection $pingableIP -Quiet -Count 1 -ErrorAction SilentlyContinue) {

                Else {

                $timestamp3 = (Get-Date -Format g)
                Add-Content -Path $offlineprinters -Value ("$timestamp3 - $serverName, $pingableIP is offline!")

                } #Else

           } #foreach -parallel ($PrinterIP in $PrinterIPs) {

        } #workflow Test-WFConnection {

        Test-WFConnection -PSComputerName $PrinterIPs

    } #If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

    Else {

        Add-Content $logfile "$serverName is offline!"

    } #Else

} #foreach ($serverName in $serverNames) {

0 个答案:

没有答案