Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File PowerShell-连续编号需要ini文件_File_Powershell_Configuration - Fatal编程技术网

File PowerShell-连续编号需要ini文件

File PowerShell-连续编号需要ini文件,file,powershell,configuration,File,Powershell,Configuration,我目前正在编写一个自动化脚本。此脚本应具有全局计数变量,该变量在再次执行脚本时不会自行重置。因此,我需要一个配置文件来存储这个count变量,并在再次调用它时使用它。此计数变量也依赖于ID。因此,每个ID都有一个计数变量。配置文件可以是XML或INI格式。有人能告诉我如何以最简单的方式创建这样的文件,以及如何添加ID或获取count变量吗?我不认为“csv导入/导出”是正确的方式 我已经试过了 $results = @() $details = @{ Key1 = $ID Key

我目前正在编写一个自动化脚本。此脚本应具有全局计数变量,该变量在再次执行脚本时不会自行重置。因此,我需要一个配置文件来存储这个count变量,并在再次调用它时使用它。此计数变量也依赖于ID。因此,每个ID都有一个计数变量。配置文件可以是XML或INI格式。有人能告诉我如何以最简单的方式创建这样的文件,以及如何添加ID或获取count变量吗?我不认为“csv导入/导出”是正确的方式

我已经试过了

$results = @()
$details = @{
    Key1 = $ID
    Key2 = $count
    Key3 = "sth"
    Key4 = "sth"
    Key5 = "sth"
    }

$results += New-Object PSObject -Property $details
$results | export-csv -Path C:\Users\...\configure.txt -NoTypeInformation
不幸的是,我无法在这里进一步了解,因为每次ID更改时它都会覆盖以前的条目,我不知道如何添加其他条目(如果ID已经存在)、更新条目(count变量)并调用此count变量以在Powershell中使用它

有人有什么建议吗

致意

您可以使用,并将您的ID计数保存并加载到XML文件:

$xmlFilePath = 'idCounts.xml'

# If the XML file exists, it is loaded
if( Test-Path -Path $xmlFilePath -PathType Leaf )
{
    $hashTable = Import-Clixml -Path $xmlFilePath
}
# Else a new hash table is initialized
else
{
    $hashTable = @{}
}

# Set the count of ID '001' to 1
$hashTable['001'] = 1

# Increment the count of ID '002'
$hashTable['002'] += 1

# Save the hash table to the XML file
$hashTable | Export-Clixml -Path $xmlFilePath
您可以使用和将ID计数保存并加载到XML文件:

$xmlFilePath = 'idCounts.xml'

# If the XML file exists, it is loaded
if( Test-Path -Path $xmlFilePath -PathType Leaf )
{
    $hashTable = Import-Clixml -Path $xmlFilePath
}
# Else a new hash table is initialized
else
{
    $hashTable = @{}
}

# Set the count of ID '001' to 1
$hashTable['001'] = 1

# Increment the count of ID '002'
$hashTable['002'] += 1

# Save the hash table to the XML file
$hashTable | Export-Clixml -Path $xmlFilePath

谢谢你给我的提示。最后,我自己通过以下方式进行管理:

if(!((import-csv "C:\Users\...\Desktop\ini.txt") | where-object {$_.Key1 -eq $ID}))
{
$results = @()

$details = @{
    Key 1 = $ID
    Key 2 = 1
    Key 3 = "something"
    Key 4 = "something"
    Key 5 = "something"
    Key 6 = "something"
    }

$results += New-Object PSObject -Property $details
$results | export-csv -Path C:\Users\...\Desktop\ini.txt -append -NoTypeInformation
}
系统首先检查是否存在具有相应ID的条目。如果没有,则创建具有该ID的对象。新创建时,计数变量设置为1。条目随“导出CSV”一起附加到文件

要使用count变量,将导入配置文件。我将其设置为“全局”,因为它必须在多个功能上运行

($csv = Import-Csv "C:\Users\...\Desktop\ini.txt") | ForEach {
    if ($_.Key1 -eq $ID) {
        $_.Key2 = $global:number}
}

$csv | Export-Csv "C:\Users\...\Desktop\ini.txt" -NoTypeInformation 
最后,count变量被更新并传输回带有“导出CSV”的文件

尽管如此,还是要感谢您提出的所有有趣的建议


向您致意

谢谢您提供的所有提示。最后,我自己通过以下方式进行管理:

if(!((import-csv "C:\Users\...\Desktop\ini.txt") | where-object {$_.Key1 -eq $ID}))
{
$results = @()

$details = @{
    Key 1 = $ID
    Key 2 = 1
    Key 3 = "something"
    Key 4 = "something"
    Key 5 = "something"
    Key 6 = "something"
    }

$results += New-Object PSObject -Property $details
$results | export-csv -Path C:\Users\...\Desktop\ini.txt -append -NoTypeInformation
}
系统首先检查是否存在具有相应ID的条目。如果没有,则创建具有该ID的对象。新创建时,计数变量设置为1。条目随“导出CSV”一起附加到文件

要使用count变量,将导入配置文件。我将其设置为“全局”,因为它必须在多个功能上运行

($csv = Import-Csv "C:\Users\...\Desktop\ini.txt") | ForEach {
    if ($_.Key1 -eq $ID) {
        $_.Key2 = $global:number}
}

$csv | Export-Csv "C:\Users\...\Desktop\ini.txt" -NoTypeInformation 
最后,count变量被更新并传输回带有“导出CSV”的文件

尽管如此,还是要感谢您提出的所有有趣的建议


致以最诚挚的问候

您可以在函数中使用它,并将其设置为常量,以便该值仅由函数更改。为什么不使用注册表项来跟踪该值?假设每个用户都是唯一的。您可以使用文本文件将值保存在中,或将其存储在数据库中。如果可能,我希望避免使用数据库。有没有简单的方法可以使用一个XML文件,该文件提供了count变量,我可以用命令轻松地更新它-(您可以在函数中使用它,并将其设置为常量,以便值仅由函数更改。为什么不使用注册表项来跟踪此情况?假设每个用户的注册表项都是唯一的。您可以使用文本文件将值保存在中,或将其存储在数据库中。如果可能,我希望避免使用数据库。是否有简单的方法使用XML文件at为我提供了count变量,我可以使用命令轻松更新该变量:-(