简体   繁体   English

提示时强制Powershell脚本继续

[英]Force Powershell script to continue when prompted

I am creating a Powershell script that uses the command net share $ShareName $ServerName /delete to stop sharing a folder. 我正在创建使用命令net share $ShareName $ServerName /delete停止共享文件夹的Powershell脚本。 The problem is, if there are open files, it will pause and require the user to press Y to continue. 问题是,如果有打开的文件,它将暂停并要求用户按Y继续。 I plan on scheduling this script to run overnight (users shouldn't be leaving stuff open overnight) and would like the script to continue automatically no matter what. 我计划将该脚本安排为在夜间运行(用户不应在一夜之间打开东西),并且希望该脚本无论如何都能自动继续运行。

Is there a way to send input to the console to imitate pressing the key, or is there some way to specify in the command to force the delete? 是否可以将输入发送到控制台以模仿按下键,还是可以在命令中指定强制删除的方法?

Try this: 尝试这个:

echo y|net share $ShareName $ServerName /delete

If you're worried about multiple prompts, just do more y s, like echo yyyyyy| 如果您担心多个提示,只需执行更多y ,例如echo yyyyyy| .

Use WMI for the operation: 使用WMI进行操作:

$share = Get-WmiObject -Computer $ServerName -Class Win32_Share -Filter "Name='$ShareName'"
$share.Delete()

Quoting from the documentation : 引用文档

The Delete WMI class method deletes a share name from a server's list of shared resources, disconnecting connections to the shared resource. Delete WMI类方法从服务器的共享资源列表中删除共享名,从而断开与共享资源的连接。

net share $ShareName $ServerName /delete /yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM