清除Powershell存储的临时项目

时间:2013-07-08 14:25:30

标签: windows powershell

我们使用此脚本尝试从我们的某些服务器获取信息。我遇到的问题是脚本以某种方式“保存”了最后一个服务器IP,因此当列表中的下一个服务器无法访问时,我们仍然可以从之前的IP获得“成功”响应。我在每次测试后尝试使用Clear-Item variable: command,但它似乎没有释放值。

$enddate = (Get-Date).tostring("yyyyMMdd")
$filename_SRM = 'C:\Scripts\' + $enddate + '_SRM_Scan.csv'

$compArray = get-content C:\Scripts\Servers.txt 
foreach($strComputer in $compArray)
{

  $ErrorActionPreference = “SilentlyContinue”
  $ping = new-object system.net.networkinformation.ping
  $ping.send(“$strComputer”) | Where-Object{$_.Name -eq 'Address'}
  $pingreturns = $ping.send(“$strComputer”).Status

  $socket = new-object Net.Sockets.TcpClient
  $port = 135  #445
  $socket.Connect($strComputer, $port)

  $Network = Get-WMIObject -class Win32_NetworkAdapterConfiguration -Computername $strComputer | `
                    Where-Object {$_.IPEnabled -match "True"} | `
                    Select-Object -property DNSHostName,@{N="DNSServerSearchOrder";
                                                        E={"$($_.DNSServerSearchOrder)"}},
                                                        @{N='IPAddress';E={$_.IPAddress}}

  $IP = [System.Net.Dns]::GetHostAddresses(“$strComputer”)

  if ($socket.Connected) 
    { 
     $Netlogon = Get-WMIObject Win32_Service -ComputerName $strComputer | Where-Object{$_.Name -eq 'Netlogon'}
     if ($Netlogon) {

         $OS  = Get-WmiObject -class Win32_OperatingSystem -computername $strComputer
         $services =  Get-WMIObject -class Win32_Service -ComputerName $strComputer 
         "$strComputer ; $pingreturns ; connected ; $($OS.CSName) ; $($Network.IPAddress) "

       }else{ 
                "$strComputer ; $pingreturns ; WMI Unavailable ;  ; $IP"
            }
         }
if (!$socket.Connected)
   {
        echo "$strComputer ; $pingreturns ; Unable to connect ;  ; $IP"
   }

   $socket.Close()
 } 

1 个答案:

答案 0 :(得分:0)

而不是Clear-Item <variable name>,请尝试Remove-Variable <variable name>,或者您可以使用语法$variable = $null。要么工作正常。