1

我编写了一个脚本,用于从 forrest/domain 收集硬件和软件信息。我已经阅读了几篇关于从服务器上的计算机运行 PS 脚本的文章,但我想做相反的事情。

你怎么知道一个脚本是“可远程访问的”。我已经看到这个命令被使用:

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

除了计算机名还有其他选择吗?我认为这不是排他性的,因为多台计算机可以具有相同的名称..

这是我的脚本:

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{
}
4

1 回答 1

2

对于-ComputerName参数,您可以使用NETBIOS 名称、IP 地址或完全限定域名。有关更多详细信息,请参阅TechNet 上的 Invoke-Command

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.

于 2012-05-03T08:51:23.850 回答