Tag Archives: powershell

Get a list of user and there Group Membership

This is how to get an CSV file, including user_SamAccountName;User_FullName;group1;group2;group3, etc

It posible to remove the comment before break, and then break the export after 5 user, to see that it function well.

It will first write it output at the ending of the script, so terminate it, will result in an empty file.

$user_count = 0
$output = ""
$OFS = "`r`n"
$users = Get-ADUser -Filter * -SearchBase "DC=dalbjerg,DC=local" | SELECT Name,samaccountname

foreach($user in $users) {
    #echo $user.SamAccountName
    $username = $user.SamAccountName
    $output += $username + ";" + $user.name + ";"
    $groups = Get-ADPrincipalGroupMembership $user.Samaccountname | select name
        foreach($group in $groups) {
            $output += $group.name + ";"
        }
    $output += $OFS
    $output += $OFS
    $user_count += 1
    if($user_count -eq 5) {
        #break
    }
}
Write-Output $output | out-file C:\temp\export.csv

 

Powershell: Copy IIS from a time range to a specific folder

I need to search for entry in logfiles between 3/8-2017 and 6/8-2017. So i make this powershell script to prevent search all IIS logfiles.

This script will copy all log files from the orginal IIS log path to a temp log directory, including the parent folder.

	$destination = "c:\temp\log\"
	$files = get-childitem "C:\inetpub\logs\LogFiles\" -Recurse | ? {$_.LastWriteTime -ge "08/02/2017" -and $_.LastWriteTime -le "08/06/2017"}
	Foreach($file in $files) {
		#Test to see if the file already exists in the destination. If not => Move/Copy/Action you want :D
		$directory = Split-Path (Split-Path $file.fullname -Parent) -Leaf
		new-item $destination"\"$directory -type directory
		Copy-Item -Path $file.fullname -Destination $destination"\"$directory

	}

Give access to calendar based on the user is member of the group.

If you need to give auser access to users calendar, based on if there are member of an group, this is the way.
Start Exchange Management Shell

$users = Get-ADGroupMember [groupname] | select -ExpandProperty name
foreach ($user in $users) {
    $Mailbox = Get-Mailbox $user
	#The Calendar, is name "Kalender" in danish
	add-MailboxFolderPermission -identity “$($Mailbox.Name):\kalender” -AccessRights Editor -User [username]
	set-MailboxFolderPermission -identity “$($Mailbox.Name):\kalender” -AccessRights Editor -User [username]

	#English calendar
	add-MailboxFolderPermission -identity “$($Mailbox.Name):\calendar” -AccessRights Editor -User [username]
	set-MailboxFolderPermission -identity “$($Mailbox.Name):\calendar” -AccessRights Editor -User [username]
} 

List all enabled user in Active Directory

How to list all enabled user in active directory:

Get-ADUser -filter {Enabled -eq $True} -Properties "DisplayName","emailaddress" | select name,emailaddress

How to list all enabled user in active directory, for a given OU:

Get-ADUser -SearchBase "OU=Accounts,OU=RootOU,DC=ChildDomain,DC=RootDomain,DC=com" -filter {Enabled -eq $True} -Properties "DisplayName","emailaddress","title" | select name,emailaddress

 

Exchange: Get a list of all email address

For en liste over alle mail adresse

Get-recipient -resultsize unlimited | select name -expand emailaddresses | select name,smtpaddress

For en liste over alle mail adresse på et given domæne

Get-Recipient | Where{$_.EmailAddresses -match "it-grp.dk"} | select name -expand emailaddresses | select name,smtpaddress | where {$_ -match "kennethdalbjerg.dk"}

XenServer: Powershell

If you like Powershell, you will like the XenServer Powershell.

Download XenServer SDK from the Citrix.com download page (You need to logon with you Citrix credentitals)

After you have install Citrix Xenserver Powershell, you can test to see if it have been install by running this comands:

Get-PSSnapin -registered

If it installed, you can add it to you powershell session by typing this command:

Add-PSSnapin Xen*

To connect to the XenServer

Connect-XenServer -Url https://<ServerIP>

To get all vm on the Xenserver:

Get-XenServer:VM

To only show the VM with VDI in the middle of the name

Get-XenServer:VM -name *vdi*

To see all command from XenServer Powershell

Get-Command -module Xen*

 

See more commands here:

http://kennethdalbjerg.dk/wp-content/uploads/2013/05/Citrix_XenServer_6.0_CmdLet_Poster.pdf
http://www.qa.com/about-qa/blogs/2012/july/citrix-xenserver-6-and-powershell/

Exchange 2010 Relay

New-ReceiveConnector -Name RelayConnector -usage Custom -Bindings ’192.168.1.10:25′ -fqdn server.domain.com -RemoteIPRanges 192.168.1.20 -server MYEXCHANGESERVER -permissiongroups ExchangeServers -AuthMechanism ‘TLS, ExternalAuthoritative’

The easist way is just to run this command in powershell:

Where:
192.168.1.10:25 is the IP address of the Exchange server
192.168.1.20 is the ip of the clients how need access to relay
server.domain.com is you exchange server FQDN
MYEXCHANGESERVER is you exchange server hostname

Change autodiscover URL (EWS URL / OAB URL / ECP URL / Activesync URL) – Exchange 2010

If you want to change Autodiscover / EWS / OAB / OWA / ECP / Activesync Url, this is how it works

Set-WebServicesVirtualDirectory -Identity “SERVERNAME\EWS (Default Web Site)” -InternalUrl https://INTERNALURL/EWS/Exchange.asmx -externalurl https://EXTERNALURL/EWS/Exchange.asmx -BasicAuthentication:$true

Set-AutoDiscoverVirtualDirectory -Identity "SERVERNAME\Autodiscover (Default Web Site)" -internalurl https://INTERNALURL/autodiscover/autodiscover.xml -externalurl https://EXTERNALURL/autodiscover/autodiscover.xml

set-OabVirtualDirectory -identity "SERVERNAME\OAB (Default Web Site)" -internalurl https://INTERNALURL/OAB -externalurl https://EXTERNALURL/OAB

set-EcpVirtualDirectory -identity "SERVERNAME\ecp (Default Web Site)" -internalurl https://INTERNALURL/ecp -externalurl https://EXTERNALURL/ecp

set-OwaVirtualDirectory -identity "SERVERNAME\owa (Default Web Site)" -internalurl https://INTERNALURL/owa -externalurl https://EXTERNALURL/owa

set-ActiveSyncVirtualDirectory  -identity "SERVERNAME\Microsoft-Server-ActiveSync (Default Web Site)" -internalurl https://INTERNALURL/Microsoft-Server-ActiveSync -externalurl https://EXTERNALURL/Microsoft-Server-ActiveSync

Set-ClientAccessServer -Identity "SERVERNAME" –AutoDiscoverServiceInternalUri https://INTERNALURL/autodiscover/autodiscover.xml

Or if this is a single server installation you can do it like this:

get-WebServicesVirtualDirectory | Set-WebServicesVirtualDirectory -InternalUrl https://INTERNALURL/EWS/Exchange.asmx -externalurl https://EXTERNALURL/EWS/Exchange.asmx -BasicAuthentication:$true

get-AutoDiscoverVirtualDirectory | Set-AutoDiscoverVirtualDirectory -internalurl https://INTERNALURL/autodiscover/autodiscover.xml -externalurl https://EXTERNALURL/autodiscover/autodiscover.xml

get-OabVirtualDirectory | set-OabVirtualDirectory -internalurl https://INTERNALURL/OAB -externalurl https://EXTERNALURL/OAB

get-EcpVirtualDirectory | set-EcpVirtualDirectory -internalurl https://INTERNALURL/ecp -externalurl https://EXTERNALURL/ecp

get-OwaVirtualDirectory | set-OwaVirtualDirectory -internalurl https://INTERNALURL/owa -externalurl https://EXTERNALURL/owa

get-ActiveSyncVirtualDirectory | set-ActiveSyncVirtualDirectory  -internalurl https://INTERNALURL/Microsoft-Server-ActiveSync -externalurl https://EXTERNALURL/Microsoft-Server-ActiveSync

get-ClientAccessServer | Set-ClientAccessServer  –AutoDiscoverServiceInternalUri https://INTERNALURL/autodiscover/autodiscover.xml