Powershell 如何使脚本在计算机上远程运行?
我编写了一个脚本,用于从forrest/域收集硬件和软件信息。我读过几篇关于从服务器上的计算机上运行PS脚本的文章,但我想做的恰恰相反 您如何知道脚本是“可远程访问”的。 我见过这个命令被使用:Powershell 如何使脚本在计算机上远程运行?,powershell,remoting,group-policy,Powershell,Remoting,Group Policy,我编写了一个脚本,用于从forrest/域收集硬件和软件信息。我读过几篇关于从服务器上的计算机上运行PS脚本的文章,但我想做的恰恰相反 您如何知道脚本是“可远程访问”的。 我见过这个命令被使用: Invoke-Command -ComputerName {serverName} –ScriptBlock { commands } 除了computername还有其他选择吗?我认为这不是排他性的,因为几台计算机可以有相同的名称 这是我的剧本: try{ $ConfirmPreferenc
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{
}
试试看{
$ConfirmPreference=“无”
$error.clear()
$erroractionpreference=“silentlycontinue”
#获取当天的日期,稍后用作XML文件的名称。
$a=获取日期-格式“%Y\u%m\u%d”
#将computername保存到compname变量(帮助器)
$compname=gc环境:计算机名称
#获取所有已保存文件和文件夹的目录路径
$scriptpath=拆分路径-父级$myinvocation.MyCommand.Definition
#PC序列号用作此计算机包含XML文件的目录的名称。
$serialnr=gwmi win32_bios |选择-展开serialnumber
#创建一个名为computers hardware serialnumber(如果不存在)的文件夹。
if(!(测试路径-路径$scriptpath\$serialnr)){
新项-路径$scriptpath-名称$serialnr-类型目录
}
#用户名
$username=gc env:username
#系统信息
gwmi-计算机$compname Win32_ComputerSystem | ForEach{$siname=$\\.Name;$simanufacturer=$\\.Manufacturer;$simodel=$\\.Model}
#图形卡
$gpuname=gwmi win32|视频控制器|选择-展开名称
#处理器信息
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”
#驱力
$totalspace=0
$totalsize=gwmi-类win32_logicaldisk
foreach($totalsize中的size){$totalspace+=$size.size}
$totalspace=“{0:N2}”-f($totalspace/1Gb)+“GB”
#windows操作系统的安装时间
$utctime=获取wmiobject win32|操作系统|选择对象-expandproperty安装日期
$installtime=[System.Management.ManagementDateTimeConverter]::ToDateTime($utctime);
$installtime=获取日期$installtime-uformat“%d/%m/%Y%X”
#--------#
#XML格式
#--------#
试一试{
$erroractionpreference=“停止”
$template=”
$serialnr
$siname
$simanufacturer
$simodel
$totalspace
$totalmem
$gpuname
$cpuname
$cpumanufacturer
cpuid
$cpucores
$cpuaddresswidth
$username
$installtime
"
$template | out文件-强制$ScriptPath\$serialnr\$a.xml
$systemroot=[System.Environment]::SystemDirectory
$xml=新对象xml
$xml.Load(“$ScriptPath\$serialnr\$a.xml”)
}抓住{
}
#OSInfo,软件
$newosinfo=(@($xml.computer.software.osinfo)[0])
获取WmiObject-计算机$compname Win32|u OperatingSystem|
ForEach对象{
$newosinfo=$newosinfo.clone()
[String]$bitversion=$\u0.o体系结构
$newosinfo.caption=[String]$\.caption++$\.osarchitecture
$newosinfo.servicepack=$\u0.csdversion
$xml.computer.software.AppendChild($newosinfo)>$null
}
$xml.computer.software.osinfo |其中对象{$\.caption-eq”“}foreach对象{$xml.computer.software.RemoveChild($)}
#-------保存并获取内容--------
$xml.Save(“$scriptpath\$serialnr\$a.xml”)
#$new=获取内容$scriptpath\$serialnr\$a.xml
#-----------------------------------
如果(!$?){
“发生了一个错误”
}
}抓住{
[系统.例外]
“脚本错误:系统异常”
}最后{
}
对于-ComputerName
参数,您可以使用NETBIOS名称、IP地址或完全限定域名。有关更多详细信息,请参阅
似乎您的脚本“仅”保存正在运行的计算机上的数据,您可能希望它返回一些有用的信息,以便使用
调用命令啊,是的,我忘了修改它以保存到远程服务器。但首先我要知道怎么做,你有什么想法吗?谢谢你的帮助!返回某些内容的最简单方法是在调用命令
前加一个变量,然后在scriptblock末尾加一个返回:$variable=Invoke命令-scriptblock{…return$something}