关于get-content获取数据的问题


PowerShell交流中心分类: Powershell基础关于get-content获取数据的问题
-1
asked 5 年 ago

param
(
[Parameter(mandatory=$true, Helpmessage=”Please provide the hostlist path”)]
[string] $hostlist
)
$computername= get-content $hostlist
$hostlist是一个带双引号的计算机列表路径名(…/**.txt)
运行时可以看见$hostlist的值为”C:\Users\**\Documents\Study\Script\Powershell\Sample\Shutdown\hostlist.txt”
但$computername 却无法通过get-content $hostlist 获取
 
尝试过输入hostlist路径名不加“”  。这样就可以,但一般输入路径名不可避免会有“”
这个问题困扰了一段时间了,希望有人能答疑解惑,感激不尽
 
 

2 Answers
1
Best Answer
六翼天使 answered 5 年 ago

param
(
[Parameter(mandatory=$true, Helpmessage=”Please provide the hostlist path”)]
[string] $hostlist
)
$computername= get-content ($hostlist.trimstart(‘”‘).trimend(‘”‘))
这样就行了,路径名有双引号的时候自动去掉,没有的时候也没事
 

replied 5 年 ago

感谢楼主!

1
answered 5 年 ago

PS C:\WINDOWS\system32> param
(
[Parameter(mandatory=$true, Helpmessage=”Please provide the hostlist path”)]
[string] $hostlist
)
Get-Content -Path $hostlist
cmdlet at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
hostlist: “C:\Users\Jacob\Documents\Study\Script\Powershell\Sample\Shutdown\hostlist.txt”
Get-Content : Cannot find drive. A drive with the name ‘”C’ does not exist.
At line:7 char:1
+ Get-Content -Path $hostlist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (“C:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

he852100 replied 4 年 ago

这是什么操作?
Get-content “c:\1.txt”
或者
$a=”c:\1.txt”
Get-content $a

Function a($b){get-content $b}
A -b “c:\1.txt”
都可以吧,都带引号。