从 .txt 文件中导入数据并在 .ps.1 文件中运行

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

Import data from .txt file and run in .ps.1 file

问题

I can help with the translation. Here is the translated content you requested:

我想导入存储在txt文件var1.txt中的PowerShell cmd到Powershell ISE中的scr3.ps1脚本并执行这些命令

var1.txt文件数据

time=(Get-Date).AddYears(-1)
extn=@(".mp3", ".jpg")
path=E:\marathi\Lavani


scr3.ps1文件数据

```powershell
$ConfigFile = "E:\marathi\Lavani\var1.txt"
# 创建一个空的哈希表
$ConfigKeys = @{}

# 从$ConfigFile中获取内容,分离并存储值在$ConfigKey中
Get-Content $ConfigFile | ForEach-Object {
    $Keys = $_ -split "="
    $ConfigKey += @{$Keys[0]=$Keys[1]}
}

$extn2 = $ConfigKey.extn
$path2 = $ConfigKey.path
$time2 = $ConfigKey.time

$time2
$extn2
$path2

Get-ChildItem -Include $extn2 -Path $path2 -Recurse | Where-Object {($_.LastWriteTime -lt $time2)}

$time2, $extn2, $path2的输出如下

(Get-Date).AddYears(-1)
@("*.mp3", "*.jpg")
E:\marathi\Lavani

但所需的输出是

2022年5月26日 上午11:32:46
*.mp3
*.jpg
E:\marathi\Lavani

如何将PowerShell命令从txt文件导入到PowerShell .ps1文件并执行这些命令?


<details>
<summary>英文:</summary>

I want import powershell cmd stored in txt file var1.txt inside a scr3.ps1 script inside Powershell ISE and execute the cmds.

var1.txt file data

time=(Get-Date).AddYears(-1)
extn=@(".mp3", ".jpg")
path=E:\marathi\Lavani


scr3.ps1 file data


$ConfigFile = "E:\marathi\Lavani\var1.txt"
#Creating an empty hash table
$ConfigKeys = @{}

#Pulling, separating, and storing the values in $ConfigKey
Get-Content $ConfigFile | ForEach-Object {

$Keys = $_ -split "="

$ConfigKey += @{$Keys[0]=$Keys[1]}
}

$extn2 = $ConfigKey.extn
$path2 = $ConfigKey.path
$time2 = $ConfigKey.time

$time2
$extn2
$path2

Get-ChildItem -Include $extn2 -Path $path2 -Recurse | Where-Object {($_.LastWriteTime -lt $time2)}



the output of $time2, $extn2, $path2 is as follows 

(Get-Date).AddYears(-1)
@(".mp3", ".jpg")
E:\marathi\Lavani


But the required output is 

26 May 2022 11:32:46 AM
*.mp3
*.jpg
E:\marathi\Lavani


how to import PowerShell command from txt file into PowerShell .ps1 file and execute the commands as well?

</details>


# 答案1
**得分**: 1

我建议你将你的txt文件转成ps1文件。

内容如下:

$time = (Get-Date).AddYears(-1)
$extn = @(".mp3", ".jpg")
$path = "E:\marathi\Lavani"


在你的主脚本的开头添加以下内容:

$ConfigFile = "E:\marathi\Lavani\var1.ps1"
.$ConfigFile


现在输出应该符合预期。

<details>
<summary>英文:</summary>

I would recommend you to turn your txt into a ps1 file. 

The content would be

    $time = (Get-Date).AddYears(-1)
    $extn = @(&quot;*.mp3&quot;, &quot;*.jpg&quot;)
    $path = E:\marathi\Lavani

And in the beginning of your main script, add this

    $ConfigFile = &quot;E:\marathi\Lavani\var1.ps1&quot;
    .$ConfigFile

Now the output should be as expected. 

</details>



huangapple
  • 本文由 发表于 2023年5月26日 14:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338141.html
匿名

发表评论

匿名网友

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

确定