none
Filename or Extension too long PowerShell error RRS feed

  • Question

  • Hi everyone,

    Let me begin by saying I don't post on forums often and I'm also very new to PowerShell. I apologize if my forum etiquette isn't up to par. I am writing a script for my first IT job that will search all AD Computers and identify which machines have Kaspersky Antivirus installed on them. The script is as follows: 

    #Retrieves a list of all computers tied to Active Directory

    $computers = Get-Content -Path 'C:\Sophos Results\AllADComputers1.txt'
    $path = Test-Path 'C:\Program Files (x86)\Kaspersky Labs'
    $path1 = Test-Path 'C:\Program Files\Kaspersky Labs'

    #Loops through each computer and looks for the Kaspersky Network Agent
    ForEach ($computer in $computers)
    {
    if ($path -eq $true -or $path1 -eq $true) 
    {Get-ADComputer $computer -Properties Name,LastLogonDate,Description | Select-Object -Property Name,LastLogonDate,Description | Export-Csv 'C:\Sophos Results\KasperskyInstalledbyPath.csv' -Append}

    else
    {Write-Host "Service is not available on $computer"}}

    When I run the script by itself nothing is output to the spreadsheet and everything is considered to not have Kaspersky. I'm guessing this has something to do with needing credentials to check each computer. So I've also created this script:

    $pw= get-content '\\DC1\scripts\opserv1.txt' | convertto-securestring -AsPlainText -Force
    $pp=new-object -typename System.Management.Automation.PSCredential -argumentlist "MyDomain\Opserv1",$pw
    $script = '\\ccbv-domctlr-01\c$\scripts\KasperskyDetectbyFilePath.ps1'
    Start-Process powershell -Credential $pp -Verbose -Argumentlist $script

    When I run this script I get the following error:

    Start-Process : This command cannot be run due to the error: The filename or extension is too long.
    At line:4 char:1
    + Start-Process powershell -Credential $pp -Verbose -Argumentlist $scri ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

    I haven't found a lot of relevant info on the internet, so I'm hoping someone on here can pinpoint exactly where I'm going wrong. Thanks very much for your help everyone.

    Thursday, June 28, 2018 5:47 PM

Answers

  • Hi Mike_T85

    On your first script it looks like you are searching the same folder on the local system where you are running the script from. You need to use \\computername\c$ to check the remote server. You could try this as long as you run with an account that has admin rights it should work. 

    #Retrieves a list of all computers tied to Active Directory
    
    $computers = Get-Content -Path 'C:\Sophos Results\AllADComputers1.txt'
    
    #Loops through each computer and looks for the Kaspersky Network Agent
    ForEach ($computer in $computers)
    {
    $path = Test-Path "\\$computer\C$\Program Files (x86)\Kaspersky Labs"
    $path1 = Test-Path "\\$computer\C$\Program Files\Kaspersky Labs"
    if ($path -eq $true -or $path1 -eq $true) 
    {Get-ADComputer $computer -Properties Name,LastLogonDate,Description | 
    Select-Object -Property Name,LastLogonDate,Description | 
    Export-Csv 'C:\Sophos Results\KasperskyInstalledbyPath.csv' -Append}
    
    else
    {Write-Host "Service is not available on $computer"}}


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by TheSleepyAdmin Thursday, June 28, 2018 5:58 PM typo
    • Marked as answer by Mike _T85 Monday, July 2, 2018 6:04 PM
    Thursday, June 28, 2018 5:57 PM
  • Hi Mike_T85 

    You need to use " quotes around \\$computername or it would see the variable.  

    $path = Test-Path "\\$computer\c$\Program Files (x86)\Kaspersky Lab"
    $path1 = Test-Path "\\$computer\c$\Program Files\Kaspersky Lab"

    Also run the test path inside the foreach loop or it won't have a value for the $computer varaible.  


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.



    • Edited by TheSleepyAdmin Monday, July 2, 2018 5:10 PM typo
    • Marked as answer by Mike _T85 Monday, July 2, 2018 6:04 PM
    Monday, July 2, 2018 5:09 PM

All replies

  • Hi Mike_T85

    On your first script it looks like you are searching the same folder on the local system where you are running the script from. You need to use \\computername\c$ to check the remote server. You could try this as long as you run with an account that has admin rights it should work. 

    #Retrieves a list of all computers tied to Active Directory
    
    $computers = Get-Content -Path 'C:\Sophos Results\AllADComputers1.txt'
    
    #Loops through each computer and looks for the Kaspersky Network Agent
    ForEach ($computer in $computers)
    {
    $path = Test-Path "\\$computer\C$\Program Files (x86)\Kaspersky Labs"
    $path1 = Test-Path "\\$computer\C$\Program Files\Kaspersky Labs"
    if ($path -eq $true -or $path1 -eq $true) 
    {Get-ADComputer $computer -Properties Name,LastLogonDate,Description | 
    Select-Object -Property Name,LastLogonDate,Description | 
    Export-Csv 'C:\Sophos Results\KasperskyInstalledbyPath.csv' -Append}
    
    else
    {Write-Host "Service is not available on $computer"}}


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by TheSleepyAdmin Thursday, June 28, 2018 5:58 PM typo
    • Marked as answer by Mike _T85 Monday, July 2, 2018 6:04 PM
    Thursday, June 28, 2018 5:57 PM
  • $computers = Get-Content -Path 'C:\Sophos Results\AllADComputers1.txt'
    $path = Test-Path 'C:\Program Files (x86)\Kaspersky Labs'
    $path1 = Test-Path 'C:\Program Files\Kaspersky Labs'
    
    #Loops through each computer and looks for the Kaspersky Network Agent
    ForEach ($computer in $computers)
    {
    if ($path -eq $true -or $path1 -eq $true) 
    this is not evaluating a remote machine; it is ONLY evaluating the machine that the script is run on.
    Thursday, June 28, 2018 6:00 PM
  • Still having the same issue. I changed my script so that it now reads as follows:

    #Retrieves a list of all computers tied to Active Directory
    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
    $path = Test-Path '\\$computer\c$\Program Files (x86)\Kaspersky Lab' 
    $path1 = Test-Path '\\$computer\c$\Program Files\Kaspersky Lab'
    #Loops through each computer and looks for the Kaspersky Network Agent
    ForEach ($computer in $computers)
    {
    if ($path -eq $true -or $path1 -eq $true) 
    {Get-ADComputer $computer -Properties Name,LastLogonDate,Description | Select-Object -Property Name,LastLogonDate,Description | Export-Csv 'C:\Sophos Results\KasperskyInstalledbyPath.csv' -Append}

    else
    {Write-Host "Service is not available on $computer"}}

    First let me note I am running PowerShell with my admin account. When I run the script by itself against a test group, it doesn't throw an error, but the results returned are "Service is not available on (computername)" Even though I've confirmed that Kaspersky is present on all 3 of my test machines. If I run just the test-path command and specify the computer name instead of using the variable, it returns true. And lastly, if I run the script through my second script, as seen again below, I still get the filename or extension too long error:

    $pw= get-content '\\DC1\scripts\opsserv1.txt' | convertto-securestring -AsPlainText -Force

    $pp=new-object -typename System.Management.Automation.PSCredential -argumentlist "MyDomain\Opsserv1",$pw

    $script = '\\DC1\c$\scripts\KasperskyDetectbyFilePath.ps1'

    Start-Process powershell -Credential $pp -Verbose -Argumentlist $script

    And the error message again:

    Start-Process : This command cannot be run due to the error: The filename or extension is too long.
    At line:1 char:1
    + Start-Process powershell -Credential $pp -Verbose -Argumentlist $scri ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand


    Monday, July 2, 2018 4:59 PM
  • Hi Mike_T85 

    You need to use " quotes around \\$computername or it would see the variable.  

    $path = Test-Path "\\$computer\c$\Program Files (x86)\Kaspersky Lab"
    $path1 = Test-Path "\\$computer\c$\Program Files\Kaspersky Lab"

    Also run the test path inside the foreach loop or it won't have a value for the $computer varaible.  


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.



    • Edited by TheSleepyAdmin Monday, July 2, 2018 5:10 PM typo
    • Marked as answer by Mike _T85 Monday, July 2, 2018 6:04 PM
    Monday, July 2, 2018 5:09 PM
  • That seems to work. Thank you so much for your responses!
    Monday, July 2, 2018 6:05 PM