Solved

Convert script between different powershell versions "exchange command shell 2013 ---> 2010"

Posted on 2016-09-23
3
43 Views
Last Modified: 2016-10-12
hi Folks,
could anybody help me to convert this powershell script to older Version? This is for Exchange command Shell 2013.
i needed for Exchange command Shell 2010.

$admodule = (get-module -ListAvailable | where {$_.name -match "ActiveDirectory"}).name
if ($admodule)
	{
		import-module activedirectory
	}
else
	{
		write-host "ActiveDirectory Modul not found" -foregroundcolor red
		exit 0
	}

clear-host
write-host "
---------------------------------------------------------------------

                    ExchangeCertificateAssistant
                      für Exchange Server 2013
                        by Frank Zöchling

                        www.FrankysWeb.de

---------------------------------------------------------------------"
write-host ""
write-host "
 Before you start ExchangeCertificateAssistent, you must create a certificate template.
" -foregroundcolor yellow
write-host ""

$ans = read-host "Did you create the Certificate template? (j/n)"

if ($ans -match "j")
	{
	}
elseif ($ans -match "n")
	{
		write-host "The template must create before" -foregroundcolor red
		exit 0
	}
else
	{
		write-host "yes or no?" -foregroundcolor red
		exit 0
	}

$template = read-host "Pls enter the name of the certificate template"	

[string]$templatepath = "ad:\CN=Certificate Templates,CN=Public Key Services,CN=Services," + (Get-ChildItem ad:\ | where {$_.name -match "Configuration"}).DistinguishedName
$templatecheck = Get-ChildItem $templatepath | where {$_.Name -match "$template"}

if ($templatecheck)
	{
		write-host " Template found" -foregroundcolor green
	}
else
	{
		write-host " Template not found" -foregroundcolor red
		exit 0
	}

$password = read-host "Pls enter the Password for the pfx" -AsSecureString

#Hostnames for the certificate
write-host ""
write-host "Get DNS-Names"
$dnsnames = $NULL

$owadirs = Get-OwaVirtualDirectory
foreach ($owadir in $owadirs)
	{
		$internal = $owadir.internalurl.host
		$external = $owadir.externalurl.host
		
		$dnsnames += @("$internal")
		$dnsnames += @("$external")
	}
	
$easdirs = Get-ActiveSyncVirtualDirectory
foreach ($easdir in $easdirs)
	{
		$internal = $easdir.internalurl.host
		$external = $easdir.externalurl.host
		
		$dnsnames += @("$internal")
		$dnsnames += @("$external")
	}

$ewsdirs = Get-WebServicesVirtualDirectory
foreach ($ewsdir in $ewsdirs)
	{
		$internal = $ewsdir.internalurl.host
		$external = $ewsdir.externalurl.host
		
		$dnsnames += @("$internal")
		$dnsnames += @("$external")
	}
	
$ecpdirs = Get-EcpVirtualDirectory
foreach ($ecpdir in $ecpdirs)
	{
		$internal = $ecpdir.internalurl.host
		$external = $ecpdir.externalurl.host
		
		$dnsnames += @("$internal")
		$dnsnames += @("$external")
	}
	
$oabdirs = Get-OabVirtualDirectory
foreach ($oabdir in $oabdirs)
	{
		$internal = $oabdir.internalurl.host
		$external = $oabdir.externalurl.host
		
		$dnsnames += @("$internal")
		$dnsnames += @("$external")
	}
	
$casservers = Get-ClientAccessServer
foreach ($casserver in $casservers)
	{
		$casshortname = $casserver.name
		$casfqdn = $casserver.fqdn
		
		$dnsnames += @("$casshortname")
		$dnsnames += @("$casfqdn")
	}

$domains = Get-AcceptedDomain
foreach ($domain in $domains)
	{
		$domainname = $domain.domainname
		$autodiscover = "autodiscover." + "$domainname"
		
		$dnsnames += @("$autodiscover")
	}		
	
$oas = Get-OutlookAnywhere
foreach ($server in $oa)
	{
		$oaintern = $server.internalhostname
		$oaextern = $server.externalhostname

		$dnsnames += @("$oaintern")
		$dnsnames += @("$oaextern")
	}
 
$dnsnames = $dnsnames | select -Unique

write-host "Following DNS-Names will be include on the certificate:"
write-host ""
$dnsnames
write-host ""

$addname = "j"
while ($addname -match "j")
{
	$addname = read-host "Should include more names to the certificate? (j/n)"
	if ($addname -match "j")
		{
			$addhost = read-host "Pls enter DNS Name"
			$dnsnames += @("$addhost")
		}
}

write-host ""
write-host "The certificate will issued with following:"
write-host ""
$dnsnames
write-host ""

#get certificate
$loc = Get-Location
Set-Location "Cert:\LocalMachine\My"
$cert = Get-Certificate -Template $template -DnsName $dnsnames -CertStoreLocation cert:\LocalMachine\My -Url ldap:
$thumbprint = $cert.Certificate.Thumbprint
Set-Location $loc

#Export certificat
$exportdir = "c:\Zertifikat"
$pathvalid = test-path $exportdir
if ($pathvalid -notmatch "True")
	{
		$folder = New-Item -ItemType directory -Path $exportdir
	}

$filepath = "$exportdir" + "\Exchange.pfx"
$export = Get-ChildItem -Path "cert:\localMachine\my\$thumbprint" | Export-PfxCertificate -FilePath $filepath -Password $password
$remove = Get-ChildItem -Path "cert:\localMachine\my\$thumbprint" | Remove-Item

#Importiere Zertifikat
$servername = $env:computername
$pfxpath = "\\" + "$servername" + "\c`$" + "\Zertifikat\Exchange.pfx"
$exservers = get-mailboxserver
$fn = "ExchangeCertificateAssistant by www.FrankysWeb.de"
foreach ($exserver in $exservers)
{
	$exservername  = $exserver.name
	write-host "Import and activate certificate on $exservername"
	$import = Import-ExchangeCertificate -Password $password -PrivateKeyExportable:$true -FileName $pfxpath -server $exservername -FriendlyName $fn
	$activate = Enable-ExchangeCertificate -Thumbprint $thumbprint -Services SMTP,IMAP,POP,IIS -force
}

write-host "
---------------------------------------------------------------------
The certificate is saved under $filepath
"

Open in new window

0
Comment
Question by:Mandy_
  • 2
3 Comments
 
LVL 42

Expert Comment

by:Amit
ID: 41812511
Did  you test same script on 2010? What error are you getting.
0
 
LVL 39

Accepted Solution

by:
Adam Brown earned 500 total points (awarded by participants)
ID: 41812586
There are no cmdlets in this that don't exist in both Exchange 2010 and 2013, as far as I can tell, so it should work just fine for 2010 as well.
0
 
LVL 39

Expert Comment

by:Adam Brown
ID: 41839769
No response. Script should work in Exchange 2010 and 2013
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange 2010 SP1 to SP3 + RU16 8 50
Cant remote powershell into server 3 22
Exchange Server 2013 crash 3 21
Exchange 2007 stray Mailbox 5 23
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

809 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