My use of Out-File and/or [system.io.file]::WriteAllText

138 views Asked by At

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"
    }
1

There are 1 answers

0
George Maxson On

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"
            }
        }
    }