2

I have a PowerShell v1 script that connects to a remote server via UNC path. After a reboot, authentication is needed because Windows apparently will not remember so the script cannot connect to the remote server

How is this situation handled programmatically from within a PowerShell script?

I need to

1) reauthenticate

2) connect to remote server via UNC path

Perhaps the "net" command???

How can I do this in my PowerShell script?

Get-ChildItem -Path "\\REMOTESERVER\Data\Files" -Filter "*.journal" | 
Where-Object { $_.Name -match 'Daily_Reviews\[\d{1,12}-\d{1,12}\].journal' } | 
Sort-Object -Property CreationTime | ForEach-Object 
{

    $sourcefile = $_.Name
    [...]


 }

Thanks

Slinky
  • 1,027
  • 3
  • 15
  • 26
  • are you using pscredentials? – tony roth Apr 16 '13 at 19:29
  • also are you not allowed to upgrade to ps v2+? – tony roth Apr 16 '13 at 19:30
  • Yes, I just upgraded ps v2 and my script currently doesn't need any credentials - Only a problem after a reboot and then windows asks the script to authenticate with the remote server. I will need to pass in credentials when I authenticate - string variables are fine for this application – Slinky Apr 17 '13 at 11:30
  • [possible duplicate](http://serverfault.com/questions/498858/reconnecting-to-a-mapped-drive-after-server-restart-from-within-powershell-scrip) – MDMoore313 Apr 17 '13 at 12:38
  • @Slinky Are you trying to connect to a File Share on the remote computer? What kind of "connection" are you using here? – Chris S Apr 17 '13 at 12:58
  • @Chris S Yes, that's exactly right. I posted a snippet above – Slinky Apr 18 '13 at 11:02
  • Who is the script running as? What do you mean "Windows wont remember" (works fine for me)? – Chris S Apr 18 '13 at 13:03
  • Script is running as admin. The problem occurs when the server where the script resides (client), reboots. Afterwards, in the file explorer the mapped drive of the remote server has an "X" through it and we need to manually reauthenticate. Would love to handle the reauth from the script – Slinky Apr 18 '13 at 19:04

1 Answers1

0

You can store credentials in your scripts. You can then use the PSCredential object with the New-PSDrive cmdlet to connect to the share. The PSDriveInfo object is then accessible to your script during the length of the session.

$username = 'domain\username'
$password = 'secret'

$password = $password | ConvertTo-SecureString -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential($username, $password)

New-PSDrive -Name journals -PSProvider FileSystem -Root '\\remoteserver\data\files' -Credential $credential | ForEach-Object { Set-Location "$_`:" }

Get-ChildItem -Filter "*.journal"
MFT
  • 400
  • 2
  • 9