Created
September 15, 2014 05:33
-
-
Save lantrix/f430d0342ceb543ca477 to your computer and use it in GitHub Desktop.
Powershell Credentials and some uses in remote file manipulation over SMB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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