Solved

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

Posted on 2016-11-17
7
67 Views
Last Modified: 2016-11-22
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
Comment
Question by:creative555
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:Chamara Weliwattage
ID: 41892232
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
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41893175
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
 

Author Comment

by:creative555
ID: 41894391
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:creative555
ID: 41895104
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
 
LVL 14

Accepted Solution

by:
Jason Crawford earned 500 total points
ID: 41895861
You misspelled Binding in the Set-Permissions function.
0
 

Author Closing Comment

by:creative555
ID: 41898204
omg. Good catch!! thanks!!!
Strange this misspelling wasn't underlined or anything in the powershell ISE....thank you so much!
0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41898416
Glad I could help.  Take care :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now