Question

I have written a script that is to collect hardware and software information from a forrest/domain. I've read several posts about running a PS-script from a computer on a server, but I want to do the opposite.

How do you know that a script is "remotely accesible". I've seen this command beeing used:

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

Is there any other alternatives than computername? I'm thinking that this is not exclussive as several computers can have the same name..

This is my script:

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{
}
Was it helpful?

Solution

For the -ComputerName parameter, you can use NETBIOS name, IP address, or fully-qualified domain name. For more details, see Invoke-Command on TechNet.

Seems like your script is "only" saving the data on the machine it is running, you will probably want it to return something in order to be useful with Invoke-Command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top