Solved

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

Posted on 2016-11-17
7
32 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

759 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

21 Experts available now in Live!

Get 1:1 Help Now