active directory - Running powershell script as multiple users -
i have different accounts server admin , workstation admin roles. want run powershell script query ad list of computers , query each computer returned check service. first part needs run server admin , second workstation admin. use 2 separate scripts. possible integrate 1 script?
here 2 scripts both run on 1 computer. first script run on workstation run server admin account 1 access active directory. script creates xml file used second script. run script workstation admin account.
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" } }
you can cycle through array of machines , use invoke-command
run scripts remotely:
$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}
update based on new question info:
you can combine scripts this:
$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" } }
Comments
Post a Comment