2012-05-03 28 views
1

我寫了一個腳本,用於從forrest/domain收集硬件和軟件信息。我已經閱讀了幾篇關於從服務器上的計算機上運行PS腳本的文章,但我想做相反的事情。如何讓腳本在計算機上運行remotelly?

你怎麼知道腳本是「遠程訪問」的。 我見過beeing使用這個命令:

Invoke-Command -ComputerName {serverName} –ScriptBlock { commands } 

是否有任何其他的選擇比計算機名?我想,這不是exclussive爲幾臺電腦可以有相同的名字..

這是我的腳本:

try{ 
    $ConfirmPreference="none" 
    $error.clear() 
    $erroractionpreference = "silentlycontinue" 

    #Gets the date of this day, used as name for XML-file later.  
    $a = get-date -uformat "%Y_%m_%d" 

    #Saves computername to compname variable(HELPER) 
    $compname = gc env:computername 

    #Gets the path to directory for all saved files and folders 
    $scriptpath = Split-Path -parent $myinvocation.MyCommand.Definition 

    #PC Serial Number, is used as name for directory containing XML files for this computer. 
    $serialnr = gwmi win32_bios | select -Expand serialnumber 

    #Creates a folder with the name of the computers hardware serialnumber if it does not exist. 
    if(!(Test-Path -path $scriptpath\$serialnr)) { 
     New-item -path $scriptpath -name $serialnr -type directory 
    } 

    #Username 
    $username = gc env:username 

    #System Info 
    gwmi -computer $compname Win32_ComputerSystem | ForEach {$siname = $_.Name; $simanufacturer = $_.Manufacturer; $simodel = $_.Model} 

    #Graphic card 
    $gpuname = gwmi win32_VideoController | select -Expand Name 

    #Processor Info 
    gwmi -computer $compname Win32_Processor | ForEach-Object {$cpuname = $_.Name; $cpumanufacturer = $_.Manufacturer; $cpucores = $_.NumberOfCores; $cpuaddresswidth = $_.AddressWidth} 

    #Memory 
    $totalmem = 0 
    $memsticks = gwmi -Class win32_physicalmemory 
    foreach ($stick in $memsticks) { $totalmem += $stick.capacity } 
    $totalmem = [String]$($totalmem/1gb) + " GB" 

    #Drive  
    $totalspace = 0 
    $totalsize = gwmi -Class win32_logicaldisk 
    foreach($size in $totalsize) { $totalspace += $size.size } 
    $totalspace = "{0:N2}" -f ($totalspace/1Gb) + " GB" 

    #Install time for windows OS 
    $utctime = get-wmiobject win32_OperatingSystem | select-object -expandproperty installDate 
    $installtime = [System.Management.ManagementDateTimeConverter]::ToDateTime($utctime); 
    $installtime = Get-Date $installtime -uformat "%d/%m/%Y %X" 


    #--------# 
    #XML-form 
    #--------# 
    try{ 
    $erroractionpreference = "stop" 
    $template = "<computer version='1.0'> 
     <hardware> 
      <serialnumber>$serialnr</serialnumber> 
      <systeminfo> 
       <name>$siname</name> 
       <manufacturer>$simanufacturer</manufacturer> 
       <model>$simodel</model> 
      </systeminfo> 
      <drive> 
       <size>$totalspace</size> 
      </drive> 
      <memory> 
       <size>$totalmem</size> 
      </memory> 
      <gpu> 
       <name>$gpuname</name> 
      </gpu> 
      <cpu> 
       <name>$cpuname</name> 
       <manufacturer>$cpumanufacturer</manufacturer> 
       <id>cpuid</id> 
       <numberofcores>$cpucores</numberofcores> 
       <addresswidth>$cpuaddresswidth</addresswidth> 
      </cpu> 
     </hardware> 
     <software> 
      <user> 
       <name>$username</name> 
      </user> 
      <osinfo> 
       <caption></caption> 
       <installdate>$installtime</installdate> 
       <servicepack></servicepack> 
      </osinfo> 
     </software> 
    </computer>" 

    $template | out-File -force $ScriptPath\$serialnr\$a.xml 
    $systemroot = [System.Environment]::SystemDirectory 
    $xml = New-Object xml 
    $xml.Load("$ScriptPath\$serialnr\$a.xml") 
    }catch{ 
    } 

    #OSInfo, software 
    $newosinfo = (@($xml.computer.software.osinfo)[0]) 
    Get-WmiObject -computer $compname Win32_OperatingSystem | 
    ForEach-Object { 
      $newosinfo = $newosinfo.clone() 
      [String] $bitversion = $_.osarchitecture 
      $newosinfo.caption = [String]$_.caption + "" + $_.osarchitecture 
      $newosinfo.servicepack = $_.csdversion 
      $xml.computer.software.AppendChild($newosinfo) > $null 
    } 
    $xml.computer.software.osinfo | where-object {$_.caption -eq ""} | foreach-object {$xml.computer.software.RemoveChild($_)} 

    #-------Save and get content-------- 
    $xml.Save("$scriptpath\$serialnr\$a.xml") 
    #$new = Get-Content $scriptpath\$serialnr\$a.xml 
    #----------------------------------- 

    if(!$?){ 
     "An error has occured" 
    } 
}catch{ 
    [system.exception] 
    "Error in script: system exception" 

}finally{ 
} 

回答

2

對於-ComputerName參數,你可以使用NETBIOS名稱,IP地址,或完全合格的域名。有關更多詳細信息,請參見Invoke-Command on TechNet

似乎您的腳本「僅」將數據保存在正在運行的計算機上,您可能會希望它返回某些內容以便與Invoke-Command一起使用。

+0

啊,是的,我忘了修改它以保存到遠程服務器。 但是,首先我需要找出如何做到這一點,從頭頂上的任何想法? 感謝您的幫助btw! – haakonlu

+0

最簡單的返回方法是在'Invoke-Command'前加一個變量,然後用一個返回來結束scriptblock:'$ variable = Invoke-Command -ScriptBlock {... return $ something}' –

相關問題