Solved

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

Posted on 2016-11-17
7
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 15

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 15

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 15

Expert Comment

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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

624 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