运行 多个用户的 powershell 脚本
Running powershell script as multiple users
我的服务器管理员和工作站管理员角色有不同的帐户。我想要 运行 一个 powershell 脚本,该脚本将查询我的广告以获取计算机列表以及每台计算机返回的查询以检查服务。第一部分需要 运行 作为服务器管理员,第二部分需要作为工作站管理员。目前我使用两个单独的脚本。是否可以将其集成到一个脚本中?
这是我的两个脚本,它们都在一台计算机上 运行。第一个脚本我 运行 在我的工作站上,但 运行 作为我的服务器管理员帐户,因为这是唯一可以访问活动目录的脚本。此脚本创建一个 XML 文件,供第二个脚本使用。我 运行 这个脚本作为我的工作站管理员帐户。
runas.exe /user:domain\srvadmin "powershell.exe -executionpolicy bypass -command c:\output\script1.ps1"
runas.exe /user:domain\wsadmin "powershell.exe -executionpolicy bypass -command c:\output\script2.ps1"
script1
import-module -name activedirectory -cmdlet get-adcomputer, get-adorganizationalunit;
$orgUnit = @("OU=Computers,DC=domain,DC=com")
$computerList = get-adcomputer -filter * -searchscope subtree -searchbase (get-adorganizationalunit $orgUnit).distinguishedname;
write $computerList | export-clixml c:\output\computerList.xml
script2
$computersInOU = import-clixml c:\output\computerList.xml
foreach ($comp in $computersInOU) {
if ($comp.Enabled) {
$cpu = get-wmiobject -class win32_processor -computername $comp.name
write "$comp.name $cpu"
}
}
您可以循环使用一系列机器并远程使用 Invoke-Command
到 运行 脚本:
$script = {Get-Process explorer}
$servers = @("Server1", "Server2") # or $servers = Get-ADComputer -filter blah1
$serverCred = Get-Credential "(Server)"
$workstations = @("WS1", "WS2") # or $workstations = Get-ADComputer -filter blah2
$workstationCred = Get-Credential "(Workstation)"
$servers | %{Invoke-Command $script -Computer $_ -Credential $serverCred}
$workstations | %{Invoke-Command $script -Computer $_ -Credential $workstationCred}
根据新问题信息更新:
您可以像这样组合您的脚本:
$srvCred = Get-Credential "domain\srvadmin"
$wsCred = Get-Credential "domain\wsadmin"
Import-Module -name ActiveDirectory -cmdlet Get-ADComputer, Get-ADOrganizationalUnit;
$orgUnit = @("OU=Computers,DC=domain,DC=com")
$searchBase = (Get-ADOrganizationalUnit -Credential $srvCred $orgUnit).distinguishedname
$computersInOU = Get-ADComputer -Credential $srvCred -filter * -searchscope subtree -searchbase $searchBase;
foreach ($comp in $computersInOU) {
if ($comp.Enabled) {
$cpu = Get-WmiObject -Credential $wsCred -class win32_processor -computername $comp.name
write "$comp.name $cpu"
}
}
我的服务器管理员和工作站管理员角色有不同的帐户。我想要 运行 一个 powershell 脚本,该脚本将查询我的广告以获取计算机列表以及每台计算机返回的查询以检查服务。第一部分需要 运行 作为服务器管理员,第二部分需要作为工作站管理员。目前我使用两个单独的脚本。是否可以将其集成到一个脚本中?
这是我的两个脚本,它们都在一台计算机上 运行。第一个脚本我 运行 在我的工作站上,但 运行 作为我的服务器管理员帐户,因为这是唯一可以访问活动目录的脚本。此脚本创建一个 XML 文件,供第二个脚本使用。我 运行 这个脚本作为我的工作站管理员帐户。
runas.exe /user:domain\srvadmin "powershell.exe -executionpolicy bypass -command c:\output\script1.ps1"
runas.exe /user:domain\wsadmin "powershell.exe -executionpolicy bypass -command c:\output\script2.ps1"
script1
import-module -name activedirectory -cmdlet get-adcomputer, get-adorganizationalunit;
$orgUnit = @("OU=Computers,DC=domain,DC=com")
$computerList = get-adcomputer -filter * -searchscope subtree -searchbase (get-adorganizationalunit $orgUnit).distinguishedname;
write $computerList | export-clixml c:\output\computerList.xml
script2
$computersInOU = import-clixml c:\output\computerList.xml
foreach ($comp in $computersInOU) {
if ($comp.Enabled) {
$cpu = get-wmiobject -class win32_processor -computername $comp.name
write "$comp.name $cpu"
}
}
您可以循环使用一系列机器并远程使用 Invoke-Command
到 运行 脚本:
$script = {Get-Process explorer}
$servers = @("Server1", "Server2") # or $servers = Get-ADComputer -filter blah1
$serverCred = Get-Credential "(Server)"
$workstations = @("WS1", "WS2") # or $workstations = Get-ADComputer -filter blah2
$workstationCred = Get-Credential "(Workstation)"
$servers | %{Invoke-Command $script -Computer $_ -Credential $serverCred}
$workstations | %{Invoke-Command $script -Computer $_ -Credential $workstationCred}
根据新问题信息更新:
您可以像这样组合您的脚本:
$srvCred = Get-Credential "domain\srvadmin"
$wsCred = Get-Credential "domain\wsadmin"
Import-Module -name ActiveDirectory -cmdlet Get-ADComputer, Get-ADOrganizationalUnit;
$orgUnit = @("OU=Computers,DC=domain,DC=com")
$searchBase = (Get-ADOrganizationalUnit -Credential $srvCred $orgUnit).distinguishedname
$computersInOU = Get-ADComputer -Credential $srvCred -filter * -searchscope subtree -searchbase $searchBase;
foreach ($comp in $computersInOU) {
if ($comp.Enabled) {
$cpu = Get-WmiObject -Credential $wsCred -class win32_processor -computername $comp.name
write "$comp.name $cpu"
}
}