Solved

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

Posted on 2016-11-17
7
75 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

791 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