Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 从服务器远程处理powershell_Xml_Powershell - Fatal编程技术网

Xml 从服务器远程处理powershell

Xml 从服务器远程处理powershell,xml,powershell,Xml,Powershell,I'va制作了一个powershell脚本,用于收集信息并将其保存到当前计算机上的XML文件中。我认为最好直接将XML保存到本地服务器。 Q:如何将XML文件保存到服务器? -以下是我保存到脚本所在目录的方式: $scriptpath=拆分路径-父级$myinvocation.MyCommand.Definition $template |输出文件$ScriptPath\tempXML.xml 问:是否可以从本地服务器运行脚本,然后将XML直接保存到该服务器上 这是我的剧本: <# FOR

I'va制作了一个powershell脚本,用于收集信息并将其保存到当前计算机上的XML文件中。我认为最好直接将XML保存到本地服务器。 Q:如何将XML文件保存到服务器? -以下是我保存到脚本所在目录的方式: $scriptpath=拆分路径-父级$myinvocation.MyCommand.Definition $template |输出文件$ScriptPath\tempXML.xml

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

#将scriptpath保存到scriptpath变量(帮助器)
$scriptpath=拆分路径-父级$myinvocation.MyCommand.Definition
#将computername保存到compname变量(帮助器)
$compname=gc环境:计算机名称
#用户名
$brukernavn=gc环境:用户名
#PC序列号
$serialnr=gwmi-计算机$compname Win32_BIOS | ForEach{$\uu.SerialNumber}
#系统信息
gwmi-计算机$compname Win32_ComputerSystem | ForEach{$siname=$\\.Name;$simanufacturer=$\\.Manufacturer;$simodel=$\\.Model}
#图形卡
gwmi“win32_VideoController”| ForEach对象{$gpuname=$\u.Name}
#处理器信息
gwmi-计算机$compname Win32|处理器| ForEach对象{$cpuname=$\.Name;$cpumanufacturer=$\.Manufacturer;$cpucores=$\\.NumberOfCores;$cpuaddresswidth=$\.AddressWidth}
#记忆
$totalmem=0
$memsticks=gwmi-类win32\u物理内存
foreach($stick in$memsticks){$totalmem+=$stick.capacity}
$totalmem=[String]$($totalmem/1gb)+“GB”
#windows操作系统的安装时间
$utctime=获取wmiobject win32|操作系统|选择对象-expandproperty安装日期
$installtime=[System.Management.ManagementDateTimeConverter]::ToDateTime($utctime);
#--------#
#XML格式
#--------#
$template=”
$serialnr
$siname
$simanufacturer
$simodel
$totalmem
$gpuname
$cpuname
$cpumanufacturer
cpuid
$cpucores
$cpuaddresswidth
$brukernavn
$installtime
"
$template |输出文件$ScriptPath\tempXML.xml
$systemroot=[System.Environment]::SystemDirectory
$xml=新对象xml
$xml.Load(“$ScriptPath\tempXML.xml”)
#硬盘
$newdrive=(@($xml.computer.hardware.drive)[0])
获取WmiObject-类Win32_logicaldisk|
ForEach对象{
$newdrive=$newdrive.clone()
$newdrive.name=[string]$\ux.name
$newdrive.volumename=[string]$\ux.volumename
$newdrive.size=“{0:N2}”-f($\u.size/1Gb)+“GB”
$xml.computer.hardware.AppendChild($newdrive)>$null
}
$xml.computer.hardware.drive |其中对象{$\.size-eq“0,00GB”-或$\.volumename-eq”“}foreach对象{$xml.computer.hardware.RemoveChild($\})
#内存信息,硬件
$newmemory=(@($xml.computer.hardware.memory)[0])
获取WmiObject-类WIN32\u物理内存|
ForEach对象{
$newmemory=$newmemory.clone()
$newmemory.PositionInRow=[string]$\ux.PositionInRow
$newmemory.datawidth=[string]$\.datawidth
$newmemory.size=[String]$($\.capacity/1gb)+“GB”
$newmemory.devicelocator=$\设备定位器
$xml.computer.hardware.AppendChild($newmemory)>$null
}
$xml.computer.hardware.memory |其中对象{$\.size-eq”“}foreach对象{$xml.computer.hardware.RemoveChild($\}
#OSInfo,软件
$newosinfo=(@($xml.computer.software.osinfo)[0])
获取WmiObject-计算机$compname Win32|u OperatingSystem|
ForEach对象{
$newosinfo=$newosinfo.clone()
[String]$bitversion=gwmi Win32_操作系统osarchitecture;
$newosinfo.caption=[String]$\.caption
$newosinfo.serialnumber=[string]$\.serialnumber+“”+$bitversion
$newosinfo.servicepack=$\u0.csdversion
$xml.computer.software.AppendChild($newosinfo)>$null
}
$xml.computer.software.osinfo |其中对象{$\.caption-eq”“}foreach对象{$xml.computer.software.RemoveChild($)}
#-------保存内容----------------
$xml.Save(“$ScriptPath\tempXML.xml”)
#-----------------------------------
试着这样做:

$xml.Save("\\$compname\c$\tempXML.xml") # this save on the root (c:\) of remote server
您需要本地管理凭据才能访问c$共享。 您可以通过另一种方式在每台服务器上创建一个共享\\server\myshareforxlm,并设置正确的权限,然后执行以下操作:

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

在某些gwmi调用中,您缺少-computername参数。这些值始终是执行脚本的本地计算机的值。谢谢Christian,但是您可以从上面的代码中详细说明路径:“\\\$compname\c$\tempXML.xml”。我真的不知道这条路径如何指向服务器上的C-disk,而不是本地机器上的C-disk。。谢谢:)您需要根据远程服务器的名称更改$compname的值!可能使用名称数组和foreach($complist中的compname){..您的实际脚本…}。看不到从$enc:computername中提取的$compname。我想它是作为param传递给script的。