如何让脚本在计算机上远程运行?

时间:2012-05-03 07:43:34

标签: powershell remoting group-policy

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

您如何知道脚本是“可远程访问”的。 我看过这个命令使用了:

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

除了computername还有其他选择吗?我认为这不是特别的,因为几台计算机可以有相同的名称..

这是我的剧本:

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

1 个答案:

答案 0 :(得分:2)

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

似乎您的脚本“仅”将数据保存在正在运行的计算机上,您可能希望它返回一些内容以便对Invoke-Command有用。