Tag Archives: Powershell

Restart a Service Remotely Using PowerShell on Multiple Servers

PowerShell

I threw a quick PowerShell function together that would allow me to restart one service on 1 or more servers in one command.  I hadn’t found an easy way to do this in PowerShell before so I threw this together.  What I did was pasted the function code into my PowerShell profile at c:\users\%USERNAME%\Documents\WindowsPowershell\Microsoft.PowerShell_profiles.ps1 to ensure it was loaded each time I started my PowerShell window.  The syntax of the script is simple:

 

Restart-MultipleServerServices -ServiceName MSExchangeTransport -ServerNames Server1,Server2

The service name is a simple string, and should be the short name of the service.  The ServerNames option should be a comma separated list of servers that you want to restart the service on.  It can be any number of servers, meaning you can use this to remotely stop the service on just 1 servers or 100.  The output of the service being restarted looks like this:

image

Copy the below script:

function Restart-MultipleServerServices([string]$ServiceName, [array]$ServerNames)

{

 

foreach ($i in $ServerNames)

{

$service = Get-Service -ComputerName "$i" -name $ServiceName

    $service.stop()

    do { Start-sleep -Milliseconds 200}

    until ((Get-Service -ComputerName "$i" -Name $ServiceName).status -eq 'Stopped')

    Write-Host "Attempting to Stop Service $($ServiceName) on Server $i" -ForegroundColor Green

    Start-Sleep 10

    $service.start()

    do { Start-sleep -Milliseconds 200}

    until ((Get-Service -ComputerName "$i" -Name $ServiceName).status -eq 'Running')

    Write-Host "Attempting to Start Service $($ServiceName) on Server $i" -ForegroundColor Green

    

    }

    }

Into your PowerShell profile at the below path.  If the PS1 file isn’t there, you can manually create it:

   1: C:\Users\%USERNAME%\Documents\WindowsPowershell\Microsoft.PowerShell_profile.ps1

image

Save and close the file and you should be ready to rock!

Disclaimer: This script is provided as is and I take no responsibility for any issue it causes in your environment.  Please test in a lab before deploying to production.

How to Import Users via CSV in Exchange 2010

exchange 2007, Exchange 2010

Create an csv file with the necessary information across the top row of the file as such:

image

The top row is going to coordinate with the S_.value that you are going to use in the following Exchange Shell command:

Import-CSV “C:Mailboxes.csv” | foreach {new-mailbox –Name $_.name –Alias $_.alias –UserPrincipalName $_.userprincipalname –Database $_.Database –OrganizationalUnit $_.organizationalunit –password (ConvertTo-SecureString $_.password –AsPlainText –force)}

image

And you should see the mailbox’s created below:

Untitled

That’s it.  You can see how the values map with their respective column names.  You can add as many users as you want, and change it so they go to different database’s.

You can even create an automated job to export from your production servers, and them import them to your DEV Exchange Servers for testing. 

How to Apply Permissions to Public Folder and All Sub Folders in Exchange 2007/2010 Using Exchange Management Shell

exchange 2007, Exchange 2010, Public Folders

 

If you have a public folder that your working on, and you need to apply permissions to it using the Exchange Management Shell, its pretty easy.  The command is:

Add-PublicFolderClientPermission –Identity “Foldername” –user UserName –AccessRights PublishingEditor

For instance, to add the user pponzeka to the folder IT with the Publishing Editor permission, the command would be the following:

13-Nov01 11.12

This works great, but what if we have several subfolders under IT, and we want to apply the same user permissions to all of the subfolders as well?  A utility called PFDAVADMIN that was available from Microsoft used to allow you to do this, and it still works with Exchange 2007.  But, since the protocol it uses, WebDAV is no longer available in Exchange 2010, we no longer have this option.  Plus, the shell is easier to use anyway!

So, we have the IT public folder, and three subfolders:

13-Nov02 11.16

So, first, in the Exchange Management Shell, if we attempt to list the public folder IT, this is the result of what we’ll see.  The command used is Get-PublicFolder –Identity “IT”

13-Nov03 11.21

That’s odd, we know there are three folders underneath, why doesn’t it list these?  We need to add the –Recurse option to our command, so that it looks in the root, and everything underneath.  So the command should be Get-PublicFolder –Identity “IT” –Recurse

13-Nov04 11.23

Notice the Parent Path?  IT has listed, which means its a Top Level Folder in Public Folders.  The other three have IT listed, which means they are sub folders of IT.

So, back to the top.  The permission to add permission on a public folder was:

Add-PublicFolderClientPermission –Identity “Foldername” –user UserName –AccessRights PublishingEditor

So in our case it was:

Add-PublicFolderClientPermission –Identity “IT” –User pponzeka –AccessRights PublishingEditor

So, now, how do we apply these permissions to the root folder, in this case IT, and all three subfolders, in this case Documents, Emails and Plans?  Well, we use the piping command to pipe the entire list of folders to the Add-PublicFolderClientPermission command.

Get-PublicFolder –Identity “IT” –Recurse | Add-PublicFolderClientPermission –User pponzeka –AccessRights PublishingEditor

13-Nov05 11.30

Note that we don’t need to specify the Identity  in the Add-PublicFolderClientPermission because we piped that to it with the | command.

And there you go.  The user account has been given these rights to the root folder, IT, and all its subfolders.  This works for any number of subfolders, and you can also use the same method to remove access rights.