Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Start-Transcript -Path "logs\last_run.log"
- Function Write-Log($string) {
- $timestamp = Get-Date -Format "s"
- Write-Host "[$timestamp]"$string
- }
- Write-Log "Loading Visual Basic"
- [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
- $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
- Write-Log "Finding servers.csv"
- if (Test-Path $scriptPath\servers.csv) {
- Write-Log "Found servers.csv in same folder"
- $csv = Import-Csv $scriptPath\servers.csv
- } elseif (Test-Path $scriptPath\..\servers.csv) {
- Write-Log "Found servers.csv in ..\"
- $csv = Import-Csv $scriptPath\..\servers.csv
- } else {
- Write-Log "Could not find servers.csv"
- [Microsoft.VisualBasic.Interaction]::MsgBox("Could not find .\servers.csv or ..\servers.csv.", 16, "Error")
- exit
- }
- Write-Log "Requesting domain credentials"
- $domainUser = [Environment]::UserDomainName+"\"+[Environment]::UserName
- $credentials = Get-Credential -Message "Domain Admin Credentials" -UserName $domainUser
- Write-Log "Requesting file name to copy"
- $fileName = [Microsoft.VisualBasic.Interaction]::InputBox("File to copy:", "Original Filename", "")
- Write-Log "Requesting destination"
- $uncPath = [Microsoft.VisualBasic.Interaction]::InputBox("Destination (no trailing slash): \\server\", "Path", "")
- $credPassword = $credentials.GetNetworkCredential().Password
- $credUser = $credentials.UserName
- if ($uncPath -and $fileName -and $credPassword -and $credUser) {
- Remove-Item $scriptPath\results.txt | Out-Null
- Write-Log "Looping over servers"
- foreach ($line in $csv) {
- $uncServer = $line.Name
- Write-Log $uncServer
- $server = "\\" + $uncServer + "\C$"
- Write-Log " Creating network drive"
- $networkLocation = New-PSDrive –Root $server -Name "PowerShell" –PSProvider FileSystem -Credential $credentials
- try {
- #$filename = "\\" + $uncServer + "\" + $uncPath + "\" + $fileName
- $newfilename = "\\" + $uncServer + "\" + $uncPath
- if (Test-Path $filename) {
- Write-Log " Copying $fileName"
- # Rename-Item $filename $newfilename
- Copy-Item $fileName $newfilename -Force
- Add-Content $scriptPath\results.txt $fileName"`r`n`t"$newfilename
- } else {
- Write-Log $uncServer":"
- }
- } catch [System.Exception] {
- Write-Log "Error: $_.Exception.Message"
- }
- Write-Log " Removing network drive"
- Remove-PSDrive "PowerShell"
- }
- Write-Log "Opening results"
- notepad.exe $scriptPath\results.txt
- } else {
- switch ("") {
- $uncPath {"Path cannot be empty."}
- $fileName {"Filename cannot be empty."}
- $uncNewFileName {"New Filename cannot be empty."}
- $credPassword {"Password not supplied."}
- $credUser {"Username not supplied."}
- }
- }
- Stop-Transcript
- Read-Host
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement