我想知道是否可以在powershell中从整个目录结构中的所有文件中递归删除特殊字符?
例如,如果我有一个名为:
my presentation (fix).xlsx
Run Code Online (Sandbox Code Playgroud)
我想将其重命名为:
my presentation fix.xlsx
Run Code Online (Sandbox Code Playgroud)
谢谢你的指导。
我们需要这样做的原因是我们正在将许多文件迁移到 sharepoint 2010 并且 sharepoint 不喜欢带有特殊字符的文件!
我编写了一个简单的 powershell 脚本来启动一个 winform(在 powershell 脚本中编写的 Winform 代码,例如 showContent.ps1 文件)并显示一些内容。我需要在 Winform 启动后隐藏 powershell.exe 命令提示符。
在网上搜索了一些案例后,我尝试了以下场景:1)我尝试在脚本文件“showContent.ps1”的开头执行“ powershell.exe -Command -windowstyle Hidden ”
2)创建一个新的脚本文件 exa:LaunchWinForm.ps1 并在其中提到命令: PowerShell.exe -windowstyle Hidden showContent.ps1
它不工作。
我正在使用 Powershell 3.0
任何人都可以告诉发生了什么问题或建议任何方法来做到这一点。
我创建了一个 powershell 脚本,可以将选定的文件复制到预定位置。
我创建了一个批处理文件来从 Windows 资源管理器的“发送到”菜单调用 Powershell 脚本。
批处理文件将选定的文件名作为参数发送给脚本。文件名可以包含空格。批处理文件代码是
powershell "C:\copycommand.ps1 '%1'"
Run Code Online (Sandbox Code Playgroud)
选择多个文件时,我希望将所有文件名作为数组发送到PowerShell脚本。
我已经编写了 powershell 脚本来接受数组,但我无法将文件名作为批处理文件中的数组发送。
下面的批处理脚本将所有文件名发送到批处理文件,但当文件名有空格时失败。
Powershell "C:\copycommand.ps1" %*
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以将所有文件名发送到 powershell 脚本。
PowerShell 没有正确显示时间并且忽略了我的本地时区 - 目前是 00:20(格林威治标准时间 +2),但是在获取时间时显示本地时间 (00:20) 但错误地打印了格林威治标准时间。
检查运行脚本的服务器,以及时区和本地时间设置是否正确。
我做错了什么还是这是一个错误?
$scriptStartDate = Get-Date
Write-Host "time is $($scriptStartDate.ToString('r'))"
# above line prints 'Sun, 14 Apr 2013 00:20:00 GMT'
# this is wrong as wrong timezone, it should be 'Sun, 14 Apr 2013 00:20:00 E.Europe'
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个新的 Win 2008 服务器本地用户并为该用户分配不同的配置文件路径。我不希望 Windows 在 C:\Users\newUser 下生成所有文件,相反,我希望它在 D:\customDir\newUser 中生成。有谁知道这样做的方法吗?
到目前为止,这就是我所拥有的:
$users= @("U1","U2","U3")
$computer = [ADSI]"WinNT://$env:COMPUTERNAME,computer"
$group = [ADSI]"WinNT://$env:COMPUTERNAME/MyCustomGroup,group"
$users | foreach {
$userName = $_
$userPath = "D:\customDir\$userName"
echo "Creating $userName..."
$user = $computer.Create("User",$userName)
$user.put("description","User $userName Description")
#$user.put("profilePath",$userPath) #This does not work, throws an error
#$user.put("homeDirDrive","D:") #This appears to be ignored when uncommented
$user.setpassword($userName)
$user.setInfo()
$group.add($user.Path)
#run cmd from user account to create profile files/folders
$spw = ConvertTo-SecureString $userName -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$spw
Start-Process cmd …Run Code Online (Sandbox Code Playgroud) profile powershell scripting user-accounts windows-server-2008
我在下面的代码中有两行 power-shell 命令。命令 1 启用邮箱,命令 2 禁用主动同步。
PSCommand psCmd = new PSCommand();
command2 ="Enable-Mailbox -Identity \""+ ExchangeIdentity
+"\" -Database \""+ExchangeDBName+ "\" -DomainController \""
+ExchangeDC+"\" -primarysmtpaddress \""+ExchangePrimarySMTPAddress +"\"";
command1 = "Set-CASMailbox -Identity \"" +ExchangePrimarySMTPAddress
+"\" -ActiveSyncEnabled "+ActiveSync_Status;
allcommand = command2 + " |" + command1;
runspace.Open();
powershell.AddScript(allcommand);
powershell.Runspace = runspace;
ICollection<PSObject> iobj = powershell.Invoke();
Run Code Online (Sandbox Code Playgroud)
当我尝试在添加脚本中单独运行命令 1 时,成功创建了邮箱。当我将它与第二个结合使用时,会单独创建邮箱,但不会禁用主动同步(如果我将值传递为 false)。
如何在一行中处理两个命令?
我是 Power Shell 的新手,我正在测试一些命令和想法。我被困在我觉得应该很简单的事情上。我想将 AD 中计算机对象的名称提取到一个文件中。到目前为止我正在尝试的方法是这个
$computers = Get-ADComputer -Filter * | Format-List name
write($computers) | Out-File -FilePath .\computers.txt
Run Code Online (Sandbox Code Playgroud)
但是我遇到的问题是输出的文件看起来像这样
name : SERVER1
name : SERVER2
name : WORKSTATION1
name : WORKSTATION2
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个直接的名字列表,没有前面的“名字:”部分。我不确定是否有更好的方法来实现这一点。
我想对用户隐藏 PowerShell Cmdlet 参数。有没有办法在 C# 中做到这一点?
我想在我的代码中使用此参数来触发具有隐藏参数的相同 cmdlet。
我使用 PowerShell 2.0。
谢谢。
我想知道如何处理对象集合的循环,以组的形式处理该集合的元素,而不是像普通 Foreach 循环那样单独处理。例如,而不是这样:
$items = get-vm
foreach ($item in $items) { do something }
Run Code Online (Sandbox Code Playgroud)
我想这样做:
$items = get-vm
foreach ((5)$item in $items) {do something}
Run Code Online (Sandbox Code Playgroud)
本质上,这个语句是想说在 items 中的 foreach 5 items 做一些工作.....
任何人都可以向我展示完成此操作所需的正确构造吗?
我需要能够在远程设备上创建一个进程并使用 PowerShell 脚本向它发送命令。我需要这样做,因为我在使用命令时会收到提示。我一直试图通过使用 Google 搜索和以下链接来弄清楚如何做到这一点:http : //tartarus.org/~simon/putty-snapshots/htmldoc/Chapter7.html#plink。通过该链接,我意识到在 Plink 中使用带有命令列表的文件将不起作用,原因如下(从该链接复制并粘贴):
您可以在服务器命令行上有效运行的任何非交互式命令,您都可以通过这种方式使用 Plink 在批处理文件中运行。
它说“非交互式命令”,这就是我正在使用的。无论如何,我也尝试使用带有命令列表的文件,但它并没有解决我的问题,因为我基本上需要发出一个命令,等待,然后在提示我时再给出一个。这是我在PuTTY 常见问题解答中找到的内容,基本上是我想尝试的内容:
可能你最好的选择是使用命令行连接工具 Plink。如果您可以将 Plink 作为第二个 Windows 进程启动,并安排您的主进程能够通过管道向 Plink 进程发送数据并从中接收数据,那么您应该能够从您的程序建立 SSH 连接。例如,这就是 CVS for Windows 所做的。
编辑:根据 user2460798 的回答,我尝试了下面的解决方案。我希望使用 PuTTY 并能够以这种方式向它发送命令。我现在的问题是我不知道如何向打开的 PuTTY 会话发送命令。所以基本上这段代码打开了一个 PuTTY 会话并尝试向它写入“ls”命令,但没有任何反应。我不知道“ls”文本的去向。
$procInfo = New-Object Diagnostics.ProcessStartInfo
$procInfo.RedirectStandardOutput=$true
$procInfo.RedirectStandardInput=$true
$procInfo.RedirectStandardError=$true
$procInfo.FileName="putty.exe"
$procInfo.Arguments="-ssh -pw <password> <username>@<device>"
$procInfo.UseShellExecute=$false
$p=[diagnostics.process]::start($procInfo)
sleep -Milliseconds 3000
$p.StandardInput.WriteLine("ls")
Run Code Online (Sandbox Code Playgroud) powershell ×10
c# ×2
activesync ×1
batch-file ×1
cmdlets ×1
file ×1
filenames ×1
foreach ×1
interactive ×1
loops ×1
plink ×1
process ×1
profile ×1
pscmdlet ×1
putty ×1
recursion ×1
scripting ×1
sharepoint ×1