0

Both commands work when running inside Visual Studio Code but if I execute the ps1 file directly in pwsh 7 the file is blank. Same if I right-click and select Open With pwsh.

Tried both commands, out-file first but moved to [system.io.file]::WriteAllText to remove blank rows. Both successfully create and populate a csv file as expected when I execute it from within Visual Studio Code.

try {
    $files = @()
    $files = Get-ChildItem -Path "\\networkShare\ps1files"
    #$files = $files + (get-childitem -Path "\\networkShare\ps1files2")
    $results = foreach ($file in $files) {
        if ($file.FullName.substring($file.FullName.length - 3, 3) -eq "ps1") {
            #Write-Host $file
            $scriptblock = [scriptblock]::Create((Get-Content -Raw -Path $file.FullName))
            $ast = $scriptblock.Ast
            $commands = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
            $commandText = foreach ($command in $commands) {
                $command.CommandElements[0].Extent.Text
            }
            $commandText | 
            Select-Object -Unique | 
            Sort-Object | 
            Select-Object @{
                Label      = "Module"
                Expression = { (Get-Command $_).Source.Substring(0, (Get-Command $_).Source.Length) }
            }

    
            $value = ""
            foreach ($result in $results) {
                if ($result.Module -inotlike "C:*") {
                    if ($value -inotlike "*" + $result.Module + "*") {
                        $value = $value + $result.Module + "`n"
                    }
                }
            }
        }
    }
    $ModulesInUse = @()
    $NewModules = @()
    $NewestMod = @()
    #$NewestMod = ""
    $ModulesInUse = Import-Csv \\networkShare\PSModulesUsed.csv -Header "Module"
    $NewModules = $results.Where({ $_.module -notin $ModulesInUse.Module -and $_.Module.Length -gt 0 -and $_.Module -inotlike "C:*" })
    if ($NewModules.count -gt 0) { 
        Write-Host "New Modules found `n"
        # Create unique array of new modules found
        foreach ( $mod in $NewModules ) {
            if ($mod.Module -notin $NewestMod.Module) {
                $NewestMod = $NewestMod + $mod
            }
        }
        $NewestMod.module
        Write-Host `n
        $Prompt = 'Do you want to update the Modules Used csv file now? Y/N'
        $UpdateFile = Read-Host -Prompt $Prompt
    
        if ($UpdateFile -eq 'y') {
            #$value | Out-File "\\networkShare\PSModulesUsed.csv"
            [system.io.file]::WriteAllText(“\\networkShare\PSModulesUsed.csv”, $value)
        }
    }

    else {
        Read-Host "No new Modules found. Press any key to exit"
    }
5
  • 1
    Out-File and [system.io.file]::WriteAllText() work...so if you're having a problem you should probably share your code. Commented Dec 19, 2022 at 22:06
  • 2
    It's propably an issue with relative paths and current directory. Try to use absolute paths everywhere. E. g. to load a file relative to your script, specify $PSScriptRoot\SomeFile.txt. This makes you independent from the current directory. Commented Dec 19, 2022 at 23:15
  • I am using a complete path for both methods: $value | Out-File "\\fileshare\folder\PSModulesUsed.csv" [system.io.file]::WriteAllText(“\\fileshare\folder\PSModulesUsed.csv”, $value) Commented Dec 20, 2022 at 1:35
  • 1
    By default, state lingers between (debugging) runs in Visual Studio Code, unfortunately. To rule that out ad hoc, restart Visual Studio Code. To rule that out methodically, configure the PowerShell extension to create a new, temporary session for every (debugging) run - see this answer Commented Dec 20, 2022 at 3:17
  • Okay, so I followed what you said mklement0 and stopped the "state linger", now writing to the file does NOT work from Code either. I am not getting any failures. I added a read-host after the save step to delay closing the session but it did NOT help. Neither did a different type of pause like timeout /t 7 /nobreak Commented Dec 20, 2022 at 14:25

1 Answer 1

0

Big thanks to mklement0!!! Making that change to Code revealed the $value I was sending was not correct, even though it was working before. When running outside of Code it was blank. I moved the code below to outside the $Results block of code and it works now.

    $value = ""
    foreach ($result in $results) {
        if ($result.Module -inotlike "C:*") {
            if ($value -inotlike "*" + $result.Module + "*") {
                $value = $value + $result.Module + "`n"
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.