1

I'va made a powershell script which collects information and saves it to an XML-file on current computer. I was thinking it was better to directly save the XML to a local server. Q: How is it possible to save XML-file to server? - This is how i'm saving to the same directory as the script: $scriptpath = Split-Path -parent $myinvocation.MyCommand.Definition $template | out-File $ScriptPath\tempXML.xml

Q: Is it possible to run the script from a local server ant then save the XML directly on there server?

This is my script:

<#
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")
#-----------------------------------
1
  • In some of gwmi calls you are missing the -computername param. The values are always of the local computer where the script is executed. Commented Apr 15, 2012 at 20:38

1 Answer 1

3

try like this:

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

You need local administrative credential to access a c$ share. You can in other way create a share in each server as \\server\myshareforxlm and setting the right permission and do:

$xml.Save("\\$compname\myshareforxml\tempXML.xml")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Christian, but can you elaborate the path from the above code: "\\$compname\c$\tempXML.xml". I really don't see how this path points to C-disk on a server and not on the local machine.. Thanks :)
You need to change the value of $compname based on the name of you remote servers! maybe with an array of name and a foreach ($compname in $complist) { ..your actual script...}. Not see the $compname is taked from $enc:computername. I thinked it was passed to script as param.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.