Tuesday, July 31, 2012

Most useful Powershell Commands for Lync and OCS

Note : Please run Powershell commands on your own risk. Author or this blogger won’t take any responsibility for the accuracy of any scripts .Please try it in your lab environment before doing it in production environment

How to get all users configured in OCS server using Lync Management shell
 (Get-CsUser -OnOfficeCommunicationServer) | ft samaccountname,Name
_______________________________
To count number of users in OCS
(Get-CsUser -OnOfficeCommunicationServer).count
Similarly :
How to get all users configured in OCS server using Lync Management shell
(Get-CsUser -OnLyncServer) | ft samaccountname,Name
To count number of users in Lync
(Get-CsUser -OnLyncServer).Count

The above will show the list of users who are enabled for OCS /Lync  with their Sam account name and their Display names
___________________________
How to finds AD disabled accounts who are still enabled for Lync.
Most of the organizations have their policy which only permits disabling a user once they resigned. If the user was enabled for OCS/Lync and their AD account got disabled, their presence will show as “Offiline” for others .Since the user is resigned we can remove them from the OCS GAL  rather than keeping them as in “Offline “ state.

1)Powershell  cmd to display users who were enabled for OCS/Lync but AD account is Disabled
Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | ft Name,Enabled,SipAddress -auto
2) Delete OCS/Lync attributes for Users who are already disabled in AD

Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq$true} | Disable-CsUser
_________________________
Easiest method for sending email from Powershell V2

send-mailmessage -from adminuser@yourdomain.com -to "user1@yourdomain.com" -subject "The Test Subject" -body "Please find the attachment " -Attachment "Attachment.txt" -smtpServer  smtp.mydomain.com

By adding the above line in a Powershell will sent an automated mail after  the script with the attachment
_________________________
Display  email address and corresponding  samaccount name of all  CS-AD-users

Get-CsAdUser | select  WindowsEmailAddress,samaccountname
_________________________
How to change Font size on OCS client, if it is accidently increased or decreased.
If font size is increased automatically or due to any accidental key stroke
CTRL+[ to increase and  CTRL+] decrease
__________________________
How to find the last reboot time of a windows systemnet statistics workstation | find "Statistics"
systeminfo | find "Up Time"
systeminfo | find "System Boot Time"
__________________________
How to remove Old OCS dialin conference numbers
In Lync Server Management Shell type
Get-CsDialInConferencingAccessNumber
and find the name corresponding to PrimaryUri :
eg: PrimaryUri : sip:dialinconference@mydomain.com
Then set the dialin Conference uri as shown below
Set-CsDialInConferencingAccessNumber -Identity sip:dialinconference@mydomain.com -ScopeToSite
Invoke-CsManagementStoreReplication   
to do the replication
_________________________
How to get details whether all Lync services are running or stopped.
get-cswindowsservice
Get the complete service list Get-service
Display services which are stopped /started
 Get-Service | Where-Object {$_.status -eq "stopped"}
 Get-Service | Where-Object {$_.status -eq "started"}

How to do a graceful reboot .
Stop-CsWindowsService -graceful
_________________________

Finds Lync users whose AD account is disabled

>Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | ft Name,Enabled,SipAddress -auto
To get the count
>(Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | ft Name,Enabled,SipAddress -auto).count
To disable them add the command at the last
>Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | ft Name,Enabled,SipAddress -auto|Disable-CsUser
__________________________
Replace or delete a repeated string from a file

$original_file = 'Filename.txt'
$destination_file =  'Filename_new.txt'
(Get-Content $original_file) | Foreach-Object {
    $_ -replace '<StringValue>', ''
    } | Set-Content $destination_file

This script will take the content of the "Filename.txt" and remove the value in the <stringValue> and create a new file without the stringValue.

__________________________
Remove space from a file

gc FilenameWithSpace.txt | where {$_ -ne ""} |out-file FilenameWithoutSpace.txt

This command will remove the space from the file and create a new file with the result

__________________________

Get the list of Lync enabled users with their email IDs
Get-CsAdUser | Where-Object {$_.Enabled -eq $True -and $_.WindowsEmailAddress -ne '' -and $_.UserAccountControl -inotlike '*disabled*'}  |FT windowsemailaddress |Out-File emailid.txt