2012-04-15 147 views
1

我做了一個powershell腳本,收集信息並將其保存到當前計算機上的XML文件。我認爲直接將XML保存到本地服務器更好。 問:如何將XML文件保存到服務器? - 這是我如何保存到與腳本相同的目錄: $ scriptpath =分割路徑-parent $ myinvocation.MyCommand.Definition $ template | out-File $ ScriptPath \ tempXML.xml從服務器遠程處理powershell

問:是否可以從本地服務器運行腳本,然後將XML直接保存在服務器上?

這是我的腳本:

<# 
FORMAT: 
- All variabel names are lowercase 
- All tags in XML are lowercase 
Some commands are run and placed directly into the XML (Commands before XML is made). 
Some commands are run after the XML is made and then adds children to specified nodes in the XML. 
#> 

#Saves the scriptpath to ScriptPath variable(HELPER) 
$scriptpath = Split-Path -parent $myinvocation.MyCommand.Definition 

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

#Username 
$brukernavn = gc env:username 

#PC Serial Number 
$serialnr = gwmi -computer $compname Win32_BIOS | ForEach {$_.SerialNumber} 

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

#Graphic card 
gwmi "win32_VideoController" | ForEach-Object {$gpuname = $_.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" 

#Install time for windows OS 
$utctime = get-wmiobject win32_OperatingSystem | select-object -expandproperty installDate 
$installtime = [System.Management.ManagementDateTimeConverter]::ToDateTime($utctime); 


#--------# 
#XML-form 
#--------# 

$template = "<computer version='1.0'> 
    <hardware> 
     <serialnumber>$serialnr</serialnumber> 
     <systeminfo> 
      <name>$siname</name> 
      <manufacturer>$simanufacturer</manufacturer> 
      <model>$simodel</model> 
     </systeminfo> 
     <drive> 
      <name></name> 
      <volumename></volumename> 
      <size></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>$brukernavn</name> 
     </user> 
     <osinfo> 
      <caption></caption> 
      <serialnumber></serialnumber> 
      <installdate>$installtime</installdate> 
      <servicepack></servicepack> 
     </osinfo> 
    </software> 
</computer>" 

$template | out-File $ScriptPath\tempXML.xml 
$systemroot = [System.Environment]::SystemDirectory 
$xml = New-Object xml 
$xml.Load("$ScriptPath\tempXML.xml") 

#Drive, hardware 
$newdrive = (@($xml.computer.hardware.drive)[0]) 
Get-WmiObject -Class Win32_logicaldisk | 
ForEach-Object { 
    $newdrive = $newdrive.clone() 
    $newdrive.name = [string]$_.name 
    $newdrive.volumename = [string]$_.volumename 
    $newdrive.size = "{0:N2}" -f ($_.size/1Gb) + " GB" 
    $xml.computer.hardware.AppendChild($newdrive) > $null 
} 
$xml.computer.hardware.drive | where-object {$_.size -eq "0,00 GB" -or $_.volumename -eq ""} | foreach-object {$xml.computer.hardware.RemoveChild($_)} 

#Memory Info, hardware 
$newmemory = (@($xml.computer.hardware.memory)[0]) 
Get-WmiObject -Class WIN32_PhysicalMemory | 
ForEach-Object { 
    $newmemory = $newmemory.clone() 
    $newmemory.PositionInRow = [string]$_.PositionInRow 
    $newmemory.datawidth = [string]$_.datawidth 
    $newmemory.size = [String]$($_.capacity/1gb) + " GB" 
    $newmemory.devicelocator = $_.devicelocator 
    $xml.computer.hardware.AppendChild($newmemory) > $null 
} 
$xml.computer.hardware.memory | where-object {$_.size -eq ""} | foreach-object {$xml.computer.hardware.RemoveChild($_)} 

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


#-------Save content---------------- 
$xml.Save("$ScriptPath\tempXML.xml") 
#----------------------------------- 
+0

在一些gwmi的叫你缺少-computername PARAM。這些值始終是執行腳本的本地計算機的值。 – 2012-04-15 20:38:48

回答

3

嘗試這樣的:

$xml.Save("\\$compname\c$\tempXML.xml") # this save on the root (c:\) of remote server 

您需要本地管理憑據訪問C $共享。 您可以在其他的方式來創建在每個服務器爲\\服務器\ myshareforxlm一定份額,並設置正確的許可,並做到:

$xml.Save("\\$compname\myshareforxml\tempXML.xml") 
+0

感謝Christian,但是您能否詳細說明上述代碼的路徑:「\\ $ compname \ c $ \ tempXML.xml」。我真的不知道這條路徑如何指向服務器上的C盤,而不是本地機器。謝謝:) – haakonlu 2012-04-16 07:55:29

+1

您需要根據遠程服務器的名稱更改$ compname的值!也許有一個名稱和一個foreach數組($ compname在$ complist中){..您的實際腳本...}。沒有看到$ compname是從$ enc:computername中提取的。我認爲它是作爲參數傳遞給腳本的。 – 2012-04-16 08:07:46