The following code will copy files to remote_computer if I use its IP address 10.10.10.10
$j = "remote_computer"
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop
Copy-Item -Path D:\ps_*able.ps1 -Destination \\10.10.10.10\share
Remove-PSDrive -name Z
This script will NOT copy over files if I use Z, the psdrive
$j = "remote_computer"
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop
Copy-Item -Path D:\ps_*able.ps1 -Destination Z
Remove-PSDrive -name Z
How to fix?
"Z" is not a valid path
Copy-Item -Path D:\ps_*able.ps1 -Destination Z:\
Related
This doesnt work
function ImportProcess{
param(
[Parameter()]
[string]$XMLPath
)
$sourcePath = $XMLPath
$DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
write-host "connecting to Dir"
New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
write-host "done"
write-host "getting all xml files"
$temp1 = Get-ChildItem -Path $sourcePath
}
I want to put this inside a function and access the files in the path and this below works. I got this methode from using credentials to get-childitem on other server
$sourcePath = "path"
$DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
write-host "connecting to Dir"
New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
write-host "done"
write-host "getting all xml files"
$temp1 = Get-ChildItem -Path $sourcePath
I tried different scopes in New-PSDrive and set-location before get-childitem, aswell as setting the Path to the Drive name + needed subdir
i removed the part where i get the credentials, because all works but as soon as i put the process in a function i get "Get-ChildItem : Access is denied"
I am trying to copy file from my local machine to a server destination
My Script: Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.X\c$'
Error:
Copy-Item : The network path was not found
At D:\PS_Test_script.ps1:1 char:2
+ Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.28X\c$'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
The server has credentials, I am guessing that, I have to invoke something to use the credentials.
To keep it somewhat simple, to copy to a folder share which requires different credentials to access you can use New-PSDrive to map a drive using those credentials
$desiredMappedDrive = 'J'
$desiredMappedDrivePath = '\\10.10.X.X\c$' # Map to administrative C: drive share requires administrator credentials)
$source = 'D:\Test.txt'
$destination = "${desiredMappedDrive}:\temp"
# Get-Credential cmdlet will request that you enter a username and password that has access to the share
New-PSDrive -Name $desiredMappedDrive -PSProvider FileSystem -Root $desiredMappedDrivePath -Credential (Get-Credential)
# after drive is mapped copy file over
Copy-Item -Path $source -Destination $destination -Verbose
Here is a whole different approach using PSSession no need to map any drives:
$targetComputerName = "127.0.0.1"
$Session = New-PSSession $targetComputerName -Credential 'username'
$DestinationPath = "C:\temp"
$source = 'D:\Test.txt'
Copy-Item -Path $source -ToSession $Session -Destination $DestinationPath
$Session | Remove-PSSession
If you want to execute it as a script you would have to create a [SecureString] and build a credential object.
this is what i would use for copying without asking for password, because i want to schedule this script
$Source = "d:\test\myZipFile.zip"
$Dest = "\\REMOTE_ip\D$\test"
$Username = "Administrator"
$Password = ConvertTo-SecureString "PasswordOFRemotePC" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
Copy-Item -Path $Source -Destination "d:\test\myZipFile1.zip"
But it gives me error
New-PSDrive : The network resource type is not correct At D:\test\copy.ps1:7 char:1
New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $m ...
+ CategoryInfo : InvalidOperation: (J:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
Going by the title of your question, I think you are overdoing this.
In your scheduled task, decide which user account should be running the script. That user should have Read access to the machine's local D:\Test folder and must have Write/Modify permissions to the remote path \\REMOTE_ip\D$\test.
You can set the task so that particular user does not even have to be logged in.
The executable to run is PowerShell.exe and the arguments are -File <PathToTheScript.ps1>
(for that, the task-running user must have Read & Execute permissions on the location of the script file)
The script itself can then be as simple as:
$Source = "d:\test\myZipFile.zip"
$Dest = "\\REMOTE_ip\D$\test"
Copy-Item -Path $Source -Destination $Dest -Force
This script is supposed to create some new drives but after i run it they dont exist, any ideas why? totally stuck....
Thanks in advance
Function New-Drives {
Param()
New-PSDrive -Name AppData -PSProvider FileSystem -Root $env:Appdata
New-PSDrive -Name Temp -PSProvider FileSystem -Root $env
$env:TEMP=Join-Path -Path C:\Windows\Temp
$mydocs=Join-Path -Path $env:userprofile -ChildPath Documents
New-PSDrive -Name Docs -PSProvider FileSystem -Root $mydocs
}
DIR temp: | measure-object –property length -sum
New-Drives
you need to add the -Persist parameter to your New-PSDrive calls. otherwise it will only create the drive within your powershell session.
I tried running this script using the ISE, and I also tried to run it on the command line as administrator. It freezes at the "Remove-ItemProperty" line. I've tried to remove that step, but then it freezes at the next step "Set-ItemProperty". It looks like the New-Item lines are working fine.
if (Test-path "HKCR:\")
{
}
else
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "npp.6.7.5.bin.zip" “C:\Notepad++”
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Remove-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)"
Set-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"
Any suggestions?
When using ItemProperty commands it interprets * as a wildcard. It's not freezing, it's searching every subkey of HKCR for "shell\Open With..." etc.
To force it to interpret the whole thing as a string path you need to use the -LiteralPath switch:
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Set-ItemProperty -LiteralPath "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"