# Error: Specified logon session does not exist.  Invoke-Command New-PSSession

**URL:** <https://forums.powershell.org/t/error-specified-logon-session-does-not-exist-invoke-command-new-pssession/12087>\
**Category:** PowerShell Help\
**Created:** [February 24, 2019, 12:10am UTC](https://forums.powershell.org/t/error-specified-logon-session-does-not-exist-invoke-command-new-pssession/12087 "2019-02-24T00:10:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [February 24, 2019, 12:10am UTC](https://forums.powershell.org/t/error-specified-logon-session-does-not-exist-invoke-command-new-pssession/12087/1 "2019-02-24T00:10:14Z")

</div>

I am trying to run a PowerShell script via a Windows 2012 server scheduled task. Script contents are below. The task connects to a list of servers, creates new-psdrive (with separate credentials), and copies files from all servers to the new psdrive (a network share). The scheduled task will run successfully only the first time. After manually starting the task again, the error below is received. I think I may have a double-hop issue (CredSSP) or scope issue. Is this a double-hop issue or should I use $global or $using scope with the new-psdrive credentials? What am I overlooking?

Window Task Scheduler details:

General Tab \> Run only when user is logged on is selected

Actions Tab: PowerShell –NoExit C:\Users\myprofile\testfile.ps1

&nbsp;

Error Received after (1) successful run:

A specified logon session does not exist. It may already have been terminated

- CategoryInfo : InvalidOperation: (MyNewPSDrive:PSDriveInfo) [New-PSDrive], Win32Exception

- FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

- PSComputerName : RemoteServerName

```
$servers = @'

server2012-3

server2012-4

server2012-5

'@ -split "`n"

 

$scripblock1 = {

# Credentials used to access network share. Password will be removed once this issue is resolved.

$pass = ConvertTo-SecureString -String 'testpassword' -AsPlainText -Force

$username = 'testusername'

#$cred = New-Object System.Management.Automation.PSCredential('testusername','testpassword')

$cred = New-Object -TypeName PSCredential($username,$pass)

New-PSDrive -Root \\ipaddress\placeholder -Name Place -PSProvider FileSystem -Credential $cred

New-Item -ItemType Directory -Path Place:\$env:COMPUTERNAME\Logs

copy-item -Path $env:USERPROFILE\DateFile.txt -Destination Place:\$env:COMPUTERNAME\Logs

}

$session = New-PSSession -ComputerName $servers

Invoke-Command -Session $session -ScriptBlock $scripblock1

Get-PSSession | Remove-PSSession
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [February 24, 2019, 3:54pm UTC](https://forums.powershell.org/t/error-specified-logon-session-does-not-exist-invoke-command-new-pssession/12087/2 "2019-02-24T15:54:34Z")

</div>

```
#Requires -Version 5

$servers = @( # How about we use an array of strings:
    'server2012-3'
    'server2012-4'
    'server2012-5'
)

Install-Module AZSBTools
Import-Module AZSBTools

# help Export-SessionCommand -ShowWindow
# help Get-SBCredential -ShowWindow

foreach ($ComputerName in $servers) {
    $session = New-PSSession -ComputerName $ComputerName   
    Export-SessionCommand -Command Get-SBCredential -Session $session
    $Cred = Get-SBCredential -UserName 'myaddomain\myusername'
    Invoke-Command -Session $session -ScriptBlock {
        New-PSDrive -Root \\ipaddress\placeholder -Name Place -PSProvider FileSystem -Credential $Using:Cred
        New-Item -ItemType Directory -Path Place:\$env:COMPUTERNAME\Logs
        copy-item -Path $env:USERPROFILE\DateFile.txt -Destination Place:\$env:COMPUTERNAME\Logs
    }
    Get-PSSession | Remove-PSSession
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/error-specified-logon-session-does-not-exist-invoke-command-new-pssession/12087/3 "2024-05-16T20:33:16Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


