如何使用vb.net运行powershell脚本文件
我有一个桌面应用程序。我正在检查所需的文本文件是否存在。如果不存在,那么我想运行一个将创建所需文本文件的PowerShell脚本。
这是我尝试的代码:
Dim userInfoPath As String = "C:\temp\UserInfo.txt"
If (IO.File.Exists(userInfoPath) = True) Then
MsgBox("The file exists !")
Else
Dim result As String
result = MsgBox("UserInfo.txt file does not exist. Click 'Yes' to create the required file.", vbYesNo)
If (result = vbYes) Then
Process.Start("powershell", "-File C:\Desktop\PowershellScript.ps1")
End If
End If
放置在C:\ desktop \ powershellscript.ps1的PowerShell文件包含脚本以创建新文件并将文本添加到它
mkdir C:\temp\
New-Item C:\temp\UserInfo.txt
Set-Content C:\temp\UserInfo.txt 'blah blah blah blah blah'
:获取错误,说对象引用未设置为对象的实例。
我在做什么错?
还有另一种运行PowerShell脚本文件的方法吗?
I have a desktop application. I am checking if the required text file exists. If it does not exist then I want to run a PowerShell script which will create the required text file.
Here's the code I am trying:
Dim userInfoPath As String = "C:\temp\UserInfo.txt"
If (IO.File.Exists(userInfoPath) = True) Then
MsgBox("The file exists !")
Else
Dim result As String
result = MsgBox("UserInfo.txt file does not exist. Click 'Yes' to create the required file.", vbYesNo)
If (result = vbYes) Then
Process.Start("powershell", "-File C:\Desktop\PowershellScript.ps1")
End If
End If
The PowerShell file placed at C:\Desktop\PowershellScript.ps1 contains the script to create a new file and add text to it:
mkdir C:\temp\
New-Item C:\temp\UserInfo.txt
Set-Content C:\temp\UserInfo.txt 'blah blah blah blah blah'
When I run the top most code and click on 'Yes' button, I get an error saying object reference not set to an instance of an object.
What I am doing wrong?
And is there another way to run a PowerShell script file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在使用
objshell.run
I'm using
objShell.Run