首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell :将主机写入当前目录/$computername

Powershell :将主机写入当前目录/$computername
EN

Stack Overflow用户
提问于 2018-03-01 16:58:31
回答 1查看 1.6K关注 0票数 1

我试图在当前目录中输出一个CSV,但是在一个与$ComputerName匹配的文件夹中,该文件夹已经存在。我经常这样做的机器列表,而不是手动放在他们的文件夹,这将是很棒的,在脚本中这样做。

下面是当前的代码,写到脚本目录。

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

我尝试过将$ComputerName添加到不同的位置,但都没有成功。

示例:

代码语言:javascript
复制
Write-Host ("Saving CSV Files at " + [Environment]::CurrentDirectory\$ComputerName + " Named the following.")
Write-Host ("Saving CSV Files at " + [Environment]::(CurrentDirectory\$ComputerName) + " Named the following.")

编辑:$ComputerName是目标的变量,而不是本地主机

EN

回答 1

Stack Overflow用户

发布于 2018-03-01 17:14:48

如果我能看到整个代码就更容易了。但我举了一个例子,因为我觉得这样解释会更容易,因为我不知道你从哪里得到变量。

非常直接地向前,它遍历计算机,如果当前文件夹中没有一个名为$computername的文件夹,它将创建一个文件夹。然后导出代码将计算机数据导出到我们刚刚创建的文件夹中。

关键部分:使用".\“的与当前文件夹相同。

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49054930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档