问题:将从文件读取的数据分配给变量。

huangapple go评论123阅读模式
英文:

Problem assign data from a file read to variables

问题

这是一个PowerShell问题,抱歉如果它出现在错误的地方。

我有一个数据文件,我将其称为PS_Upgrade_Data。它列出了6个项目,如下所示:

item1=item-2=item.3=item 4=item5=item6

请注意,项目2到4之间的写法是由于数据来自我无法控制的原因。项目2中有一个破折号,项目3中有一个句点,项目4中有一个空格,只是为了清楚起见。项目1、5和6在单词"item"和数字之间没有任何内容。

我正在使用以下PowerShell命令来从文件中读取数据:

Get-Content "P:\PS_Upgrade_Data.txt" | Where-Object {$.length -gt 0} | Where-Object {!$.StartsWith("#")} | Foreach-Object -begin {$count1=0} -process {$var = $_.Split('=',6).Trim()}

这确实从文件中读取了数据。

我想要将这些数据放入六(6)个不同的变量中,我尝试了一个类似以下的循环:

$count1 = 0
ForEach ($item in $var) {
Write-Host "`n"
Write-Host "count = " , $count1
if($count1 = 0) {
Write-Host "UserInit"
$UserInit = $item
}
elseif ($count1 = 1) {
Write-Host "UserInit"
$TicketNumber = $item
}
elseif ($count1 = 2) {
Write-Host "UserInit"
$UpgradeVersion = $item
}
elseif ($count1 = 3) {
Write-Host "UserInit"
$ClientName = $item
}
elseif ($count1 = 4) {
Write-Host "UserInit"
$InstName = $item
}
elseif ($count1 = 5) {
Write-Host "UserInit"
$Coffee = $item
}
$count1 +=1
}

问题是计数器从0(零)跳到2,然后不再增加,我不知道为什么。

我做了什么愚蠢的事情或者漏掉了什么?

感谢任何帮助。

英文:

This is a PowerShell question, sorry if it wound up in the wrong place.

I have a data file which I will call PS_Upgrade_Data. It has 6 items in it listed like this:

item1=item-2=item.3=item 4=item5=item6

Please note that items 2 through 4 are written that way due to the data coming in over which I have no control. There is a dash in item2, a period in intem3 and a space in item4 just to be clear. Items 1, 5, and 6 have nothing between the word item and the number.

I am using the follow PS line to read the data in from the file:

Get-Content "P:\PS_Upgrade_Data.txt" | Where-Object {$_.length -gt 0} | Where-Object {!$_.StartsWith("#")} | Foreach-Object -begin {$count1=0} -process {$var = $_.Split('=',6).Trim()}

This does read the data from the file in ok.

I want to take that data and drop it into six (6) different variables and I tried a foreach loop like the one below:

		$count1 = 0
	ForEach ($item in $var) {
		Write-Host "`n"
		Write-Host "count = " , $count1
		if($count1 = 0) {
			Write-Host "UserInit"
			$UserInit = $item
		}
		elseif ($count1 = 1) {
			Write-Host "UserInit"
			$TicketNumber = $item
		}
		elseif ($count1 = 2) {
			Write-Host "UserInit"
			$UpgradeVersion = $item
		}
		elseif ($count1 = 3) {
			Write-Host "UserInit"
			$ClientName = $item
		}
		elseif ($count1 = 4) {
			Write-Host "UserInit"
			$InstName = $item
		}
		elseif ($count1 = 5) {
			Write-Host "UserInit"
			$Coffee = $item
		}
		$count1 +=1
		}

The problem is that the counter is jumping from 0 (zero) to two (2) and then wont increase and I have no idea why.

What stupid thing am I doing or missing?

Thanks for any help.

答案1

得分: 1

PowerShell的赋值运算符=支持一次操作多个赋值目标,因此您可以跳过计数器和foreach循环,而是简单地执行以下操作:

Get-Content "P:\PS_Upgrade_Data.txt" | Where-Object {$_.length -gt 0} | Where-Object {!$_.StartsWith("#")} | Foreach-Object {
  $UserInit,$TicketNumber,$UpgradeVersion,$ClientName,$InstName,$Coffee = $_.Split('=',6).Trim()

  # 在这里使用您的变量进行操作
}
英文:

PowerShell's assignment operator = supports multiple assignment targets per operation, so you can skip the counter and foreach loop and instead, simply do:

Get-Content "P:\PS_Upgrade_Data.txt" | Where-Object {$_.length -gt 0} | Where-Object {!$_.StartsWith("#")} | Foreach-Object {
  $UserInit,$TicketNumber,$UpgradeVersion,$ClientName,$InstName,$Coffee = $_.Split('=',6).Trim()

  # do with your variables what you want here
}

huangapple
  • 本文由 发表于 2023年2月9日 00:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定