Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Query AD over a persistent session - "Not enough storage is available to complete this operation."

$
0
0

 
# Name : ListComputers.ps1
# Purpose: Get all active / inactive computers from Active Directory
#

param (
    [string]$server = "$(Read-Host 'Server')"
)

# Get user credentials
$cred = Get-Credential

# get today's date
$today = Get-Date

#Get today - 60 days (2 month old)
$cutoffdate = $today.AddDays(-60)

# Create session
$session = New-PSSession -Computer $server -Credential $cred

# Load module in remote session
Invoke-Command -ScriptBlock { import-module activedirectory } -Session $session

# Connect remote session module commands locally
Import-PSSession -Session $session -Module activedirectory -Prefix rem

# IMPORTANT:
#   You cannot use variables (i.e. $cutoffdate) when using Get-remADComputer
#   Variables live in the local Powershell instance and do not carry across to the remote system.
#   You have to build the entire command string first, expanding all variables into plain text(!)
#   Building a command string requires escaping special characters that are to be treated as literal characters.
# ESCAPING:
# Double quotes (") are escaped with a back-quote (`)
# Back quotes (`) {= used for command line continuation on a new line} are escaped with a back-quote (`)
# Dollar symbols ($) are escaped with a back-quote (`)
# Note that variable $cutoffdate is not escaped as we need the variable value, not the variable name.

[string]$command = "Get-remADComputer -Properties * -Filter {LastLogonDate -gt `"$cutoffdate`"} ``
| Select Name,OperatingSystem,OperatingSystemVersion, ``
LastLogonDate,CanonicalName | Export-Csv ./ActiveComputers.csv -NoTypeInformation"

Write-Debug $command

Invoke-Expression $command

[string]$command = "Get-remADComputer -Properties * -Filter {LastLogonDate -lt `"$cutoffdate`"} ``
| Select Name,OperatingSystem,OperatingSystemVersion, ``
LastLogonDate,CanonicalName | Export-Csv ./InActiveComputers.csv -NoTypeInformation"

Write-Debug $command

Invoke-Expression $command

# Cleanup
Get-PSSession | Remove-PSSession

==================================

 

 

The first AD query completes successfully, as the list of active computers is fairly short due to the Xmas holidays.  The second AD query does not complete successfully - possibly due to the larger dataset:

From inside powershell.exe:

Starting a command on remote server failed with the following error message :
The I/O operation has been aborted because of either a thread exit or an application request. For more information, see the
about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: (192.168.1.10:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : 192.168.1.10

From inside PowerShellPlus:

Processing data for a remote command failed with the following error message: Not enough storage is available to complete this operation.
For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: (System.Manageme...pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRemotingTransportExce
   ption
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : 192.168.1.10

Starting a command on remote server failed with the following error message : The WS-Management service cannot process the request because
 the request contained invalid selectors for the resource. For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: (System.Manageme...pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRemotingTransportExce
   ption
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : 192.168.1.10

Note that I am deliberately running this script from a client instead of from a server.
[I want to create a script that runs from anywhere and operates on multiple workstations/servers using multi threaded remoting (-asjob).  Before getting to that stage I need to query AD to build the list of hostnames to operate on]


Viewing all articles
Browse latest Browse all 10624

Trending Articles