【问题标题】:Remoting powershell from server从服务器远程处理powershell
【发布时间】:2012-04-16 01:30:24
【问题描述】:

我制作了一个 powershell 脚本,它收集信息并将其保存到当前计算机上的 XML 文件中。我在想直接将 XML 保存到本地服务器会更好。 问:如何将 XML 文件保存到服务器? - 这就是我保存到与脚本相同的目录的方式: $scriptpath = 拆分路径-parent $myinvocation.MyCommand.Definition $模板 |输出文件 $ScriptPath\tempXML.xml

问:是否可以从本地服务器 ant 运行脚本,然后将 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")
#-----------------------------------

【问题讨论】:

  • 在某些 gwmi 调用中,您缺少 -computername 参数。这些值始终是执行脚本的本地计算机的值。

标签: xml powershell


【解决方案1】:

试试这样:

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

您需要本地管理凭据才能访问 c$ 共享。 您可以通过其他方式在每个服务器中创建共享 \\server\myshareforxlm 并设置正确的权限并执行以下操作:

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

【讨论】:

  • 感谢 Christian,但您能否详细说明上述代码中的路径:“\\$compname\c$\tempXML.xml”。我真的不明白这条路径如何指向服务器上的 C 盘,而不是本地机器上。谢谢 :)
  • 您需要根据远程服务器的名称更改 $compname 的值!也许有一个名称数组和一个foreach($complist中的$compname){..你的实际脚本...}。看不到 $compname 取自 $enc:computername。我认为它是作为参数传递给脚本的。
猜你喜欢
  • 2013-11-07
  • 1970-01-01
  • 2011-04-23
  • 2016-08-13
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
相关资源
最近更新 更多