Remove-Variable - 找不到具有名称的变量

时间:2018-01-15 00:45:12

标签: powershell powershell-v5.0

我有一个PowerShell脚本,其中包含以下df <- setDT( structure(list(idno = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), day = c("Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday"), clockst = c(1.3, 1.5, 2, 4, 10.25, 10.45, 11.3, 12.05, 12.15, 12.35, 14, 14.15, 14.4, 15.45, 15.5, 16.05, 16.1, 16.3, 16.5, 17.4, 17.55, 18.25, 19, 19.2, 19.35, 20, 20.25, 20.3, 20.55, 21.1, 21.35, 22.3, 23.15, 23.3, 0.15, 1.5, 2, 4, 10.25, 10.45, 11.3, 12, 12.1, 12.3, 12.5, 13.05, 13.55, 14.3, 14.5, 15.5, 16, 16.1, 17.4, 17.55, 18.25, 18.3, 20, 20.15, 20.2, 20.55, 21.1, 21.3, 21.45, 22, 23.15, 0.1, 1, 1.1, 4, 12.05, 12.25, 13, 13.2, 14.3, 15.35, 16, 16.3, 17, 18.3, 19.3, 20, 22, 23.1, 23.3, 1, 1.15, 4, 11.05, 11.3, 11.4, 12, 12.15, 13, 14, 14.3, 15.2, 17, 17.3, 18.3, 19.3, 21.2, 21.35, 21.5, 22, 22.1, 22.55, 23.05, 23.3), start = c(1290L, 1310L, 1320L, 0L, 385L, 405L, 450L, 485L, 495L, 515L, 600L, 615L, 640L, 705L, 710L, 725L, 730L, 750L, 770L, 820L, 835L, 865L, 900L, 920L, 935L, 960L, 985L, 990L, 1015L, 1030L, 1055L, 1110L, 1155L, 1170L, 1215L, 1310L, 1320L, 0L, 385L, 405L, 450L, 480L, 490L, 510L, 530L, 545L, 595L, 630L, 650L, 710L, 720L, 730L, 820L, 835L, 865L, 870L, 960L, 975L, 980L, 1015L, 1030L, 1050L, 1065L, 1080L, 1155L, 1210L, 1260L, 1270L, 0L, 485L, 505L, 540L, 560L, 630L, 695L, 720L, 750L, 780L, 870L, 930L, 960L, 1080L, 1150L, 1170L, 1260L, 1275L, 0L, 425L, 450L, 460L, 480L, 495L, 540L, 600L, 630L, 680L, 780L, 810L, 870L, 930L, 1040L, 1055L, 1070L, 1080L, 1090L, 1135L, 1145L, 1170L), end = c(1310L, 1320L, 1440L, 385L, 405L, 450L, 485L, 495L, 515L, 600L, 615L, 640L, 705L, 710L, 725L, 730L, 750L, 770L, 820L, 835L, 865L, 900L, 920L, 935L, 960L, 985L, 990L, 1015L, 1030L, 1055L, 1110L, 1155L, 1170L, 1290L, 1310L, 1320L, 1440L, 385L, 405L, 450L, 480L, 490L, 510L, 530L, 545L, 595L, 630L, 650L, 710L, 720L, 730L, 820L, 835L, 865L, 870L, 960L, 975L, 980L, 1015L, 1030L, 1050L, 1065L, 1080L, 1155L, 1215L, 1260L, 1270L, 1440L, 485L, 505L, 540L, 560L, 630L, 695L, 720L, 750L, 780L, 870L, 930L, 960L, 1080L, 1150L, 1170L, 1210L, 1275L, 1440L, 425L, 450L, 460L, 480L, 495L, 540L, 600L, 630L, 680L, 780L, 810L, 870L, 930L, 1040L, 1055L, 1070L, 1080L, 1090L, 1135L, 1145L, 1170L, 1260L), time = c(20L, 10L, 120L, 385L, 20L, 45L, 35L, 10L, 20L, 85L, 15L, 25L, 65L, 5L, 15L, 5L, 20L, 20L, 50L, 15L, 30L, 35L, 20L, 15L, 25L, 25L, 5L, 25L, 15L, 25L, 55L, 45L, 15L, 120L, 95L, 10L, 120L, 385L, 20L, 45L, 30L, 10L, 20L, 20L, 15L, 50L, 35L, 20L, 60L, 10L, 10L, 90L, 15L, 30L, 5L, 90L, 15L, 5L, 35L, 15L, 20L, 15L, 15L, 75L, 60L, 50L, 10L, 170L, 485L, 20L, 35L, 20L, 70L, 65L, 25L, 30L, 30L, 90L, 60L, 30L, 120L, 70L, 20L, 40L, 15L, 165L, 425L, 25L, 10L, 20L, 15L, 45L, 60L, 30L, 50L, 100L, 30L, 60L, 60L, 110L, 15L, 15L, 10L, 10L, 45L, 10L, 25L, 90L)), .Names = c("idno", "day", "clockst", "start", "end", "time"), row.names = c(NA, -108L), class = "data.frame", index = structure(integer(0), "`__start`" = c(4L, 38L, 69L, 87L, 5L, 39L, 6L, 40L, 88L, 7L, 41L, 89L, 90L, 42L, 91L, 8L, 70L, 43L, 9L, 92L, 71L, 44L, 10L, 45L, 72L, 93L, 46L, 73L, 47L, 11L, 94L, 12L, 48L, 74L, 95L, 13L, 49L, 96L, 75L, 14L, 15L, 50L, 51L, 76L, 16L, 17L, 52L, 18L, 77L, 19L, 78L, 97L, 98L, 20L, 53L, 21L, 54L, 22L, 55L, 56L, 79L, 99L, 23L, 24L, 80L, 100L, 25L, 26L, 57L, 81L, 58L, 59L, 27L, 28L, 29L, 60L, 30L, 61L, 101L, 62L, 31L, 102L, 63L, 103L, 64L, 82L, 104L, 105L, 32L, 106L, 107L, 83L, 33L, 65L, 34L, 84L, 108L, 66L, 35L, 67L, 85L, 68L, 86L, 1L, 2L, 36L, 3L, 37L), "`__idno`" = integer(0))) )

hashtable

在我的脚本中的某个点后,我不再需要[HashTable]$folder_and_prefix = @{} ,我尝试了:

hashtable

但是我收到以下错误:

  

删除变量:找不到具有名称的变量   &#39; System.Collections.Hashtable&#39;

是否有可能删除我不再需要的hastable?

2 个答案:

答案 0 :(得分:6)

这是一个常见的错误。 Remove-Variable正在获取变量的名称(a [String]),并通过引用变量本身(带有美元符号$)来传递< EM>值。删除美元符号,这就是你所需要的:

Remove-Variable folder_and_prefix

此外,它需要一系列名称,因此您可以这样做:

$var1 = 5
$var2 = 'Hello'
$var3 = @{}

Remove-Variable var1,var2,var3

它接受通配符:

Remove-Variable var*

最后,对于Set-VariableGet-VariableClear-Variable也是如此(New-Variable是例外情况)。

答案 1 :(得分:0)

正如其他人提到的那样,在引用变量本身时,请不要使用美元符号($)。另外,我总是使用如下条件语句检查变量是否存在(尝试删除它之前):

if (Test-Path variable:myVar) {Remove-Variable myVar}  #Delete only if local variable exists.
[int]$myVar = 5;

您还可以检查变量在特定范围中的存在,例如全局范围:

if (Test-Path variable:global:myVar) {Remove-Variable myVar –Scope Global} 

您甚至可以如下检查环境变量:

if (Test-Path env:ComputerName) {"do something with $env:ComputerName."}

HTH