Skip to content

Instantly share code, notes, and snippets.

@lantrix
Created September 15, 2014 05:33
Show Gist options
  • Save lantrix/f430d0342ceb543ca477 to your computer and use it in GitHub Desktop.
Save lantrix/f430d0342ceb543ca477 to your computer and use it in GitHub Desktop.
Powershell Credentials and some uses in remote file manipulation over SMB
$User = 'machine\administrator'
$Password = 'abcd1234!'
$host = 10.0.0.10
$securedPassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($User, $securedPassword)
#establish session
$session = New-PSSession -ComputerName $host -Credential $Credential -ErrorAction SilentlyContinue
#Setup Source/Destination
$source = "C:\folder\" #with trailing backslash
$destinationDrive = "C" #raw drive letter only
$destinationPath = "folder" # without trailing backslash or drive e.g.: path\to\copy\folder
$destination = '\\' + $host + '\' + $destinationDrive + '$' + '\' + $destinationPath + '\'
$destinationDrive = '\\' + $host + '\' + $destinationDrive + '$'
New-PSDrive -Name X -PSProvider FileSystem -Root $destinationDrive -Credential $Credential
#Remove remote directory
Remove-Item $destination -Force -Recurse
New-Item $destination -type directory
#copy files
$files = Get-ChildItem -Path $source -Recurse -Include '*.sql'
foreach ($this in $files) {
if ($this.PSIsContainer) {
$thisdest = Join-Path $destination $this.Parent.FullName.Substring($source.length)
} else {
$thisdest = Join-Path $destination $this.FullName.Substring($source.length)
}
Copy-Item $this -Destination $thisdest -Force
}
#unmap drive
Remove-PSDrive -Name X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment