2

After connecting to remote computer and executing some powershell job, I need to save a text file with output to a remote server using powershell.

My file name is created depending on computer name and date.

$filename = '' + $enddate + '' + $name + ''

Output is:

$output = "\\10.0.50.8\Informatyka\Posnet_raporty"

There is a username and password to connect to folder "Posnet_raporty"

$username = "user"
$password = "pass"

I use 2 functions to create the file and have been trying to use the NET USE command to create a disc on the remote computer to save the file there, but it has failed ;/ (already mapped source from this adress, net use don't like it)

If ([int]$end1 -gt 30) {
    $end1, $end2 | Out-File Q:\$filename'.txt'
}
else {
    $end1, $end2 | Out-File Q:\'@'$filename'.txt'
}

The output of $end1 is a number (this does not apply to the question, but I prefer to write everything I can)

edit: net use fail, can not work with this same source what is already mapped on computers. PSDrive is only accessible in PowerShell session.

fosfik
  • 83
  • 8
  • 1
    it would be helpful to see the error messages and more details about "connect to remote computer". Is this PS Remoting or SMB connection? – maoizm Mar 23 '18 at 13:08
  • I'm looking a option to save it on remote server with username and password to access to it. I don't have any error :) I need a working idea ^^ Maybe someone have a experience with similiar problem or can show me nice solution. – fosfik Mar 23 '18 at 13:12
  • then check accepted answer at https://stackoverflow.com/questions/303045/connecting-to-a-network-folder-with-username-password-in-powershell if I understand you correctly, your question should be marked as a possible duplicate – maoizm Mar 23 '18 at 13:28

1 Answers1

0

I'm not totally clear on what you're trying to do, but if you want to map to a specific remote folder, you might want to use a PSDrive. These are like traditional mapped drives, but are by default only accessible in your PowerShell session. You can create one like this:

$userName = "domain\user"
$password = "Password"

$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)

New-PSDrive -Name Q `
            -PSProvider FileSystem `
            -Credential $cred `
            -Root \\ServerName\Share\Folder1\Folder2

The credentials are optional - the mapping will work ok if the current user already has permission to access the location.

You can then use it like any other drive:

Get-ChildItem Q: -File -Recurse

Disconnect afterwards, by doing this:

Remove-PSDrive -Name Q
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • I have error trying this: `New-PSDrive : Permission denied At C:\scripts\scanner\test.ps1:83 char:1 + New-PSDrive -Name "Q" -PSProvider "FileSystem" -Root "\\10.0.50.8\Inf ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Q:PSDriveInfo) [New-PSDrive], Win32Exception + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand` – fosfik Mar 23 '18 at 14:13
  • `New-PSDrive -Name "Q" -PSProvider "FileSystem" -Root "\\10.0.50.8\Informatyka" -Persist -Credential $cred` this is my line – fosfik Mar 23 '18 at 14:14
  • My username from `$username` have permission to write & read in `\\10.0.50.8\Informatyka\Posnet_raporty` – fosfik Mar 23 '18 at 14:24
  • Is 'Informatyka' a shared folder? What permission do you have to that? Also, can you test it against something like the C$ admin share, or with a different account? – boxdog Mar 23 '18 at 14:37
  • Ughh.. My admin account work great with this :) So I need to change something with the user what I use to login here. Btw... thx a lot :) Thats the solution what I was looking for. – fosfik Mar 23 '18 at 14:44