Quantcast
Channel: Group Policy forum
Viewing all articles
Browse latest Browse all 19997

GPO Startup policy failed to run Powershell script

$
0
0

Dear fox,

I have created powershell script to rename more than 1000 computers host name same as active directory user account. If i run the policy manually in my computer then it successfully changed but if i deploy from GPO in computer startup script it doesn't work.

$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {

   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

   $newProcess.Arguments = $myInvocation.MyCommand.Definition;

   $newProcess.Verb = "runas";

   [System.Diagnostics.Process]::Start($newProcess);

   exit
   }
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$name = $env:username
$computerName = Get-WmiObject Win32_ComputerSystem
$computername.Rename($name)
write-Host "$([char]1) Computer Name is changed to `"$name`", I am Going to Reboot Laptop after 10 seconds."  -ForegroundColor Green
write-host "$([char]7) IT Department has changed your computername same as your email address." -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 9 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 8 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 7 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 6 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 5 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 4 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 3 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 2 Seconds" -ForegroundColor Green
sleep 1
write-host "$([char]7) Computer will restart in 1 Seconds" -ForegroundColor Green
sleep 1
Restart-Computer -Force

### End of Script ## 

second issue is when i run the same script on same computer but with different test user, it was failed with return error code 5 in powershell console.


Viewing all articles
Browse latest Browse all 19997

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>