Invoke-Command:空值和位置参数错误

时间:2016-11-17 23:08:35

标签: powershell

我想为此开始一个新线程,因为我现在在我的代码中使用不同的方法。我编写了一个脚本,可以ping数百个设备并记录其在线或离线状态。这需要很长时间才能运行,所以我现在正在研究使用Invoke-Command在每个站点的服务器上远程运行命令(而不是来自同一服务器的所有站点)。我收到以下错误:“找不到接受参数'Of'”,“你不能在空值表达式上调用方法”的位置参数,并且每个服务器在迭代它们时都会发生这种情况。关于为什么会发生这种情况的任何想法?非常感谢,这是我目前的代码:

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

#Define log file variables and remove any existing logs
$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 "Gathering server list"

#Compiling list of all MPOS Print Servers
$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"

Add-Content $logfile "Compiling text file"

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

Invoke-Command -ComputerName $serverNames -ScriptBlock {

#Define log file variables and remove any existing logs
$logfile = "C:\Temp\MPOSPrinterPingLog.txt"
$offlineprinters = "C:\Temp\MPOSOfflinePrinters.txt"
$masteroffline = "\\a0345p689\d$\Reports\MPOS\MPOSOfflinePrinters.txt"
If (Test-Path $logfile) {Remove-Item $logfile}
If (Test-Path $offlineprinters) {Remove-Item $offlineprinters}

#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:\ProgramData\Microsoft\Point Of Service\Configuration\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}} 

foreach ($PrinterIP in $PrinterIPs) {

    $pingableIP = $PrinterIP.HardwarePath

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

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

            }

    Else {

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

            }

    Get-Content $offlineprinters | Out-File -FilePath $masteroffline -Append -NoClobber

} #foreach ($PrinterIP in $PrinterIPs) {

} #Invoke-Command -ComputerName $serverNames -ScriptBlock {

0 个答案:

没有答案