On
previous article, we did talk about storing credentials securely and re-using it within PowerShell script.
To follow up I would like to show you what NOT TO DO
Don’t do this; it is way too obvious why you should not be doing this, I am not going to waste time to sell this out.
$MyPassword = ConvertTo-SecureString "My password" -AsPlainText -Force $MyUsername = "My username"
|
Usage within the script;
$csvFile = "C:\foldermigration.csv" $table = Import-Csv $csvFile -Delimiter "," $MyPassword = ConvertTo-SecureString "My password" -AsPlainText -Force $MyUsername = "My username" Set-Variable dstSite, dstList foreach ($row in $table) { Clear-Variable dstSite Clear-Variable dstList $dstSite = Connect-Site -Url $row.ONEDRIVEURL -UserName $MyUsername -Password $MyPassword Add-SiteCollectionAdministrator -Site $dstSite $dstList = Get-List -Name Documents -Site $dstSite Import-Document -SourceFilePath "C:\Migrated data" -DestinationList $dstList Remove-SiteCollectionAdministrator -Site $dstSite }
|
To be fair to ShareGate, it is one of the best third-party utility for SharePoint and OneDrive migrations.
SG PowerShell module is pretty efficient, and its software has tons of great features with some limitations.
Do this instead
$adminName = ‘adminName’
$credpath = "${env:\userprofile}\Documents\$adminName.Credential"
$Credential = Get-Credential $adminName
$Credential | Export-CliXml -Path $credpath
$CredObject = Import-CliXml -Path $credpath
|
To make it bit more organized
#(1)_.Define Credentials Vars
$adminName = 'admin'
$OrgName = 'CloudSec365.onmicrosoft.com'
$Const = '@'
$adminUPN = $adminName+$Const+$OrgName
$credpath = "${env:\userprofile}\Documents\Credentials\"
$FileName = $adminName + '.credential'
$credfile = $credpath+$FileName
$now = (get-date -format 'dd-MMM-yyyy-HH-mm-ss')
|
#Create folder to store credentials
Function Function-create-Folder {
[CmdletBinding()]
param()
Try{
if (!(Test-Path -Path $credpath))
{
New-Item -Type Directory -Path $credpath -ErrorAction Stop | Out-Null
}
}catch{
$errormessage = $($PSItem.ToString())
Write-Warning 'Error has occoured'
Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
}
}
|
#(-)_.Function-Create-Secure-File
Function Function-Create-Secure-File {
[CmdletBinding()]
param()
Try{
$Credential = Get-Credential $adminUPN -ErrorAction Stop
$Credential | Export-CliXml -Path $credfile
}catch{
$errormessage = $($PSItem.ToString())
Write-Warning 'Error has occoured'
Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
}
}
|
#(-)_.Import the file to Use
$CredObject = Import-CliXml -Path $credfile
#(-)_.Function-remove-Files
Function Function-remove-Files {
[CmdletBinding()]
param()
Try{
Remove-Item -Path $credpath -Recurse -Force # Remove Folder
# Get-ChildItem $credpath -Recurse -force | Remove-Item -Force # remove files
}catch{
$errormessage = $($PSItem.ToString())
Write-Warning 'Error has occoured'
Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
}
}
|
Casey DeDeal
Azure Certified Solutions Architect
AWS Certified Cloud Practitioner
https://msazure365.blogspot.com
https://simplepowershell.blogspot.com
https://twitter.com/Message_Talk