Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Powershell script- running set owner command on multiple servers at once

Hello,
WE have been using this script to set permissions on each server and it is working great.

Is there a way, to do the same but specify multiple servers and run it once for multiple servers?
What would be the powershell alternative for this one? All servers are 2012 R2 and powershell remoting is enabled on all



o      Set E:\ and F:\ file permissions w/ TESTTARGET\username1, TESTTARGET\username2, and TARGET\ADMS.Administrators (see below ICACLS command)
o      Set E:\ and F:\ owner to username (see below ICACLS command)
icacls E:\ /grant TESTTARGET\username1:(OI)(CI)F
icacls E:\ /grant TESTTARGET\username2:(OI)(CI)F
icacls F:\ /grant TESTTARGET\username1:(OI)(CI)F
icacls F:\ /grant TESTTARGET\username2:(OI)(CI)F
icacls E:\ /grant TESTTARGET\ADMS.Administrators:(OI)(CI)F
icacls F:\ /grant TESTTARGET\ADMS.Administrators:(OI)(CI)F
icacls "E:\Includes" /setowner TESTTARGET\username1 /t /c
icacls "E:\ADMS" /setowner TESTTARGET\username1 /t /c
icacls "E:\Program Files" /setowner TESTTARGET\username1 /t /c
icacls "F:\Software" /setowner TESTTARGET\username1 /t /c
0
creative555
Asked:
creative555
  • 3
  • 3
1 Solution
 
Chamara WeliwattageCommented:
Hi,
Create a function and run it as "Jobs" in power shell using Start-Job comlet.

Sample:
Start-Job -InitializationScript $func -ScriptBlock {all-info -server <ServerName>}

$func = script to run in each server.
$all-info = calling "all-info" function.
-Server = is the parameter for specifying server name where the script to be run.

I had 14 exchange server where each server has 5000~ mailboxes. Normal script take 12-14hrs to get all the info.
After running the script parallel on all server, it get all the mailbox information in 1.5 hrs.

This is challenging, but you can do it.
0
 
Jason CrawfordExchange EngineerCommented:
When using the Invoke-Command cmdlet, PowerShell will 'fan' out to all servers at the same time.  Try this:

1. Save your list of servers in a file entitled servers.txt on your desktop
2. Run the script below:

function Set-Permissions {
  icacls E:\ /grant TESTTARGET\username1:(OI)(CI)F
  icacls E:\ /grant TESTTARGET\username2:(OI)(CI)F
  icacls F:\ /grant TESTTARGET\username1:(OI)(CI)F
  icacls F:\ /grant TESTTARGET\username2:(OI)(CI)F
  icacls E:\ /grant TESTTARGET\ADMS.Administrators:(OI)(CI)F
  icacls F:\ /grant TESTTARGET\ADMS.Administrators:(OI)(CI)F
  icacls "E:\Includes" /setowner TESTTARGET\username1 /t /c
  icacls "E:\ADMS" /setowner TESTTARGET\username1 /t /c
  icacls "E:\Program Files" /setowner TESTTARGET\username1 /t /c
  icacls "F:\Software" /setowner TESTTARGET\username1 /t /c 
}

Invoke-Command -ComputerName (Get-Content "$env:USERPROFILE\Desktop\servers.txt") -ScriptBlock {Set-Permissions}

Open in new window


If you want to test the script first, just replace the Set-Permissions function with 'hostname' (no quotes).  If everything goes according to plan, the hostname for all servers in your .txt file will be the only output.
0
 
creative555Author Commented:
Oh. I got this error....this script is not working....please help

The term 'Set-Permissions' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Set-Permissions:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
creative555Author Commented:
Hello,
I am new to powershell. Was doing lynda training all today. So, I wrote this function. Unfortunately the training has PS v3 and I have PS v5.
I am getting this error: Cannot find the type for custom attribute 'CmdletBidning'. Make sure that the assembly that contains this type is loaded.

Please help. I googled it and still can't resolve it...



Function Set-Permissions {
[CmdletBidning()]
    param([Parameter(Mandatory=$true,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [Alias('hostname')]
    [string[]$CommputerName,
    [switch]$nameLog
)
BEgin {}
Process {
    foreach ($computer in $computername)
{
#write-host $computer  
    icacls E:\ /grant TESTTARGET\TEST.Administrators:(OI)(CI)F
    icacls F:\ /grant TESTTARGET\TEST.Administrators:(OI)(CI)F
    icacls "E:\Includes" /setowner TESTTARGET\test.svc /t /c
    icacls "E:\ADMS" /setowner TESTTARGET\test.svc /t /c
    icacls "E:\Program Files" /setowner TESTTARGET\test.svc /t /c
    icacls "F:\Software" /setowner TESTTARGET\test.svc /t /c
   
}

}

End {}


}



Set-Permissions -computername .\computers

Invoke-Command -ComputerName (Get-Content ".\Computers.txt") -ScriptBlock {Set-Permissions}
0
 
Jason CrawfordExchange EngineerCommented:
You misspelled Binding in the Set-Permissions function.
0
 
creative555Author Commented:
omg. Good catch!! thanks!!!
Strange this misspelling wasn't underlined or anything in the powershell ISE....thank you so much!
0
 
Jason CrawfordExchange EngineerCommented:
Glad I could help.  Take care :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now