I am having an issue to connect to the remote server using New-PSDrive. The remote server is Windows-based, and only userA has access to write.
By saying that, the following code throws an "access denied" error:
Access to the path '$remoteServerPath' is denied on line3
Code:
New-PSDrive -Name remote -Root $remoteServerPath -PSProvider FileSystem
$destination = [IO.Path]::Combine('remote:', $fileName)
Copy-Item -Path $source -Destination $destination -Force
Now, I am trying to include credential information, but I get a different error!
The network path was not found on line3
$secpass = ConvertTo-SecureString 'myPassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('domain\userA', $secpass)
New-PSDrive -Name remote -Root $remoteServerPath-PSProvider FileSystem -Credential $cred
$destination = [IO.Path]::Combine('remote:', $fileName)
Copy-Item -Path $source -Destination $destination -Force
Can anyone please help me out? Powershell Ver. 5
Why are you creating a PSDrive for this task?
& NET USE Z: \\server\path /user:domain\UserA 'PASSWORD'
Copy-Item -Path $Source -Destination 'Z:\' -Force
& NET USE Z: /D
If you have their plaintext password, this should work just fine.
Related
How does one connect anonymously to an SMB share in powershell using New-PSDrive?
I've tried omitting the -Credential param but this seems to use the currently logged in user. This works when I test using a domain account, however the problem is for normal operation the currently logged in user is a local kiosk user for assigned access that the domain file server does not recognize.
I've also tried using the following, however it prompts for user input. As this is run as a scheduled task for background operation - this is unacceptable.
$Credentials = Get-Credential -UserName 'NTAUTHORITY\Anonymous Logon'
New-PSDrive -ErrorAction Stop -PSProvider "FileSystem" -Root "$RemoteFolder" -Name "$RemoteDriveLetter" -Credential $Credentials -Persist -Scope Global | Out-Null
I have enabled the local security policy option on the file server for "Network access: Let Everyone permissions apply to anonymous users".
How do I utilize the "anonymous" user connection with New-PSDrive?
-- edit --
I've also tried this
$Credentials = [pscredential]::Empty
New-PSDrive -ErrorAction Stop -PSProvider "FileSystem" -Root "$RemoteFolder" -Name "$RemoteDriveLetter" -Credential $Credentials -Persist -Scope Global | Out-Null
However, the output is:
>> TerminatingError(New-PSDrive): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The specified network password is not correct"
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The specified network password is not correct
Anonymous mounts use an 'empty' user and password for the credential block so you can do the same.
This works for me and allows file creation on the share:
$User = " " # Create 'empty' username
$PWord = ConvertTo-SecureString -String " " -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
New-PSDrive -PSProvider "FileSystem" -Root "$RemoteFolder" -Name "$RemoteDriveLetter" -Credential $Credentials -Persist -Scope Global | Out-Null
I used the powershell script below to try and create a folder on a remote machine
$ComputerName = "<IP>"
$DriveLetter = "C"
$Path = "TempFolder\TestPath"
New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force
When I change the ComputerName to localhost, the script is executed and the intended folder is created. However, when I run this script for an IP in the same network, which I can ping, the script runs but the folder isn't created.
Do you need to provide login credentials for the remote machine somewhere in the script?
Indeed you do:
PS C:> help new-item
NAME
New-Item
SYNTAX
New-Item [-Path] <string[]> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf]
[-Confirm] [-UseTransaction] [<CommonParameters>]
New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential
<pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
ALIASES
ni
the important part is -Credential <pscredential>. To create this object from plain text for example:
$password = ConvertTo-SecureString 'password' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('username', $password)
There are other more secure ways to do this if you search around the net on how to create a PSCredential object.
I am trying to perform a silent install of an application. (I'm doing this so that I can deploy it silently to other users in our domain)
The program I'm trying to silently install is OpenVPN. Here is the PowerShell script that I'm currently attempting to run.
$Computer = Read-Host -Prompt 'Enter the Computer Name you are accessing'
########## Install Software On PC ##########
New-Item -ItemType directory -Path "\\$Computer\c$\temp\openVPN"
Copy-Item "Z:\(15) IT\VPN\openvpn-install-2.4.8-1602-Win7.exe" "\\$Computer\c$\temp\openVPN" -Recurse
Write-Host "Installing openVPN on $Computer"
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process "c:\temp\openVPN\openvpn-install-2.4.8-1602-Win7.exe" -ArgumentList "/q" -Wait}
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process "c:\temp\openVPN\openvpn-install-2.4.8-1602-Win7.exe" -ArgumentList "/q" -Wait}
########## Remove temporary files and folder on each PC ##########
Write-Host "Removing Temporary files on $Computer"
$RemovalPath = "\\$Computer\c$\temp\openVPN"
Get-ChildItem -Path $RemovalPath -Recurse | Remove-Item -Force -Recurse
Remove-Item $RemovalPath -Force -Recurse
But I am receiving a couple of error messages when I run the script. The first error I encounter is that it says is
Copy-Item : Cannot find path 'Z:\(15) IT\VPN\openvpn-install-2.4.8-1602-Win7.exe' because it does not exist
(It does exist) And then the 2nd error I receive is
Connecting to remote server (Computer Name) failed with the following error message : The client cannot connect to the destination specified in the request.
Any help is appreciated!
I am trying to import a certificate on a VM.
I am doing the following script:
Start-VM -Name $NewVmName
Copy-VMFile -Name $NewVmName -SourcePath ".\certificate.pfx" -DestinationPath "C:\certificate.pfx" -CreateFullPath -FileSource Host
$Password = "test" | ConvertTo-SecureString -asPlainText -Force
Invoke-Command -VMName $NewVmName -Credential $LocalCredential -ScriptBlock {
Import-PfxCertificate -FilePath "C:\certificate.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password $Password
}
So the certificate store location is indicated. However, when I run this script, Windows prompts me to know in which store I want to import the certificate:
How come? what do I do wrong? I want the certificate to be imported without any user interactions of course.
Thanks!
I need to connect to a remote server and do some file copies and moves, etc. The remote server requires authentication.
What UNC path do I need to enter in for PSCredential? Is it the path to a particular remote user like
\SERVER2\Users\Administrator
Or is it the path to the remote resource itself like
\SERVER2\Data\Content
...
Authenticate the call
$Creds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist "C:\Some\Path",$PW
Copy-Item $src $destination -Credential $Creds
It is the username. Your paths are not proper UNC paths however. They should start with two slashes like \\SERVER2\Users\Administrator
Specify the credentials of the user with permission to access the resource. The Get-Credential commandlet can get the credentials for you.
$cred = Get-Credential
Copy-Item $src $destination -Credential $cred