Thursday, April 30, 2020

PowerShell function to prompt GUI for admins to select input file – import CSV;

PowerShell function to prompt GUI for admins to select input file – import CSV;
If you are looking for a function to prompt users to select input file instead of defining a variable, here is one way of doing it. This function is very useful and it will help your administrators not to make mistake, assuming correct input file is selected.




#(1)-.Function to Prompt GUI to select input file
$InitialDir  = "C:\Users\$env:USERNAME\Documents\CSV_Files\" 


#(2)_. Create CSV folder
If (!(Test-Path $InitialDir)) {
New-Item -ItemType Directory -Force $InitialDir | Out-Null
}

Function Get-FileName ($InitialDir)
 [System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms'| Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $InitialDir
$OpenFileDialog.filter = 'All files (*.*)| *.*'
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}


#(3)_.Start reading CSV file
write-host 'You will need to locate <CSV> file for bulk import' -ForegroundColor Yellow
read-host 'Press <ENTER> to contiue'
$csvfile = Get-FileName -initialDirectory $InitialDir
$Mylist  = Import-Csv -path $csvfile




Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate


Sunday, April 26, 2020

Using PowerShell to validate variable;

Using PowerShell to validate variable;
Here is one simple way to check if $variable does have value or not. This way is pretty simple and will be handy when adding  checks into your scripts.



   
# check if $myvar has a value
$myvar = 'DATA'
if(!$myvar){ Write-Host 'NULL' -ForegroundColor Red }


# Same check adding else;
if (!$myvar){

Write-Host 'NULL' -ForegroundColor Red

}else{

Write-Host 'NOT NULL!!!' -ForegroundColor Green

}


# Check if $myvar has <ANY> value except $null
$myvar
if($myvar){ Write-Host 'My var value is <NULL>' -ForegroundColor Red }


# Same check adding else;
if ($myvar){

Write-Host 'NULL' -ForegroundColor Red

}else{

Write-Host 'NOT NULL!!!' -ForegroundColor Green

}




Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate




Wednesday, April 22, 2020

Using PowerShell to store credentials in a secure string Part#2

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