1

I'm trying to output a CSV in the current directory, but in a folder that matches the $ComputerName, which already exists. I'm doing this for a list of machines regularly and rather than manually put them in their folder it would be awesome to do it in the script.

Here is the current code, writing to the script directory.

#writing file to 
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath 
Write-Host ("Saving CSV Files at " + [Environment]::CurrentDirectory + " Named the following.")
Write-Host $PritnersFilename

I've tried adding $ComputerName to various locations and had no luck.

Examples:

Write-Host ("Saving CSV Files at " + [Environment]::CurrentDirectory\$ComputerName + " Named the following.")
Write-Host ("Saving CSV Files at " + [Environment]::(CurrentDirectory\$ComputerName) + " Named the following.")

EDIT: $ComputerName is the variable of the target, not the local host

Indigo
  • 21
  • 2

2 Answers2

1

It would be easier if I saw the whole code. But I made up an example because I felt it would be easier to explain it likes this since I don't know where you get your variables from.

Pretty straight forward, it loops through the computers and if there isn't a folder in current folder named $computername it creates one. Then your export code comes in where it exports computer data to that folder we just created.

Key part: Using ".\" is the same thing as current folder.

cd C:\Scriptfolder\

# computer variables for example
$computers = @()
$computers += "HOST1"
$computers += "HOST2"
$computers += "HOST3"

# looping through all objects
Foreach($computer in $computers){

    # creating folder named after computername if one doesn't exist
    if(!(Test-Path ".\$computer")){
        New-Item -ItemType Directory -Name $computer -Path ".\"
    }
    # output path with computername in it
    $outputpath = ".\$computer\output.csv"

    # your export code
    $computer | Export-CSV $outputpath
}
bostrom
  • 11
  • 1
1

[Environment]::CurrentDirectory\$ComputerName, due to being inside (...) and being used as an operand of the + operator, is parsed in expression mode, which causes a syntax error.

For an overview of PowerShell's parsing modes, see this answer of mine.

You need "..." (an expandable string) to perform your string concatenation, using subexpression operator $(...) to embed expression [Environment]::CurrentDirectory and embedding a reference to variable $ComputerName directly.

"$([Environment]::CurrentDirectory)\$ComputerName"

For an overview of string expansion (string interpolation) in PowerShell, see this answer of mine.

Alternatively, you could use an expression with + as well (or even mix the two approaches):

# Enclose the whole expression in (...) if you want to use it as a command argument.
[Environment]::CurrentDirectory + '\' + $ComputerName

Note: The most robust (albeit slower) method for building filesystem paths is to use the Join-Path cmdlet:

# Enclose the whole command in (...) to use it as part of an expression.
Join-Path ([Environment]::CurrentDirectory) $ComputerName

Note the need for (...) around [Environment]::CurrentDirectory to ensure that it is recognized as an expression. Otherwise, since the command is parsed in argument mode, [Environment]::CurrentDirectory would be treated as a literal.

mklement0
  • 382,024
  • 64
  • 607
  • 775