英文:
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 = @("*.mp3", "*.jpg")
$path = E:\marathi\Lavani
And in the beginning of your main script, add this
$ConfigFile = "E:\marathi\Lavani\var1.ps1"
.$ConfigFile
Now the output should be as expected.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论