标签: powershell

如何使用 PnP PowerShell 检索大型 SharePoint 库中的所有列表项

我有一个图书馆,里面有大约 25k 个项目。我有一个 csv,其中有几个文件夹,我想向它们授予项目级别权限。但首先,要做到这一点,我需要从图书馆中检索所有项目。正如预期的那样,我收到有关管理员强制执行的阈值限制的错误。我想知道是否有办法通过 PnP 解决这个问题。

我基本上曾经Get-PnPListItem -List Documents-PageSize 1000尝试限制一次返回的项目数量,但我仍然收到错误消息。我也尝试了下面的代码,但caml query;也不起作用。

$filename = test.docx
Get-PnPListItem -List Documents -Query
"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/>
<Value Type='File'>' +$filename+ '</Value></Eq></Where></Query></View>"
Run Code Online (Sandbox Code Playgroud)

我只期望显示一个结果,即在文档库中找到文件名时。所以我不确定为什么会显示错误消息。有什么想法吗?

powershell sharepoint-online

3
推荐指数
1
解决办法
1万
查看次数

比较 LastWriteTime 和 CreationTime

如果文件被创建并写入一次,我期望 LastWriteTime 与 CreationTime 相同。这段代码似乎表明这不是真的。这些时间戳存储的精度是否更高?ToString 和比较是唯一的方法吗?

PS 09:30  C:\src\t
>Get-ChildItem .\uu.txt | Select-Object -Property Name,LastWriteTime,CreationTime

Name   LastWriteTime       CreationTime
----   -------------       ------------
uu.txt 2019-08-09 09:23:25 2019-08-09 09:23:25


PS 09:30  C:\src\t
>Get-ChildItem .\uu.txt | ForEach-Object { $_.LastWriteTime -eq $_.CreationTime }
False
Run Code Online (Sandbox Code Playgroud)

更新:

我最终得到了以下代码。如果 LastWriteTime 和 CreationTime 之间的差异小于一秒,则假定文件自创建以来尚未更新。

$CloseEnough = New-Timespan -Seconds 1
Get-ChildItem -File -Path 'C:\src\t' |
    Where-object { ($_.LastWriteTime - $_.CreationTime) -lt $CloseEnough }
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
5715
查看次数

使用 New-ScheduledTaskTrigger 注册计划任务以在事件 ID 上触发

在 Windows 事件 ID 上使用 New-ScheduledTaskTrigger 注册 ScheduledTask

堆栈溢出的用户大家好。MSDN 和 Google 都没有产生结果...
我通过 Powershell 脚本配置了几个计划任务。计划任务设置为在特定时间运行。
这一切都很好。但我需要配置另一个计划任务,该任务在 Windows 事件记录器中记录某个事件 ID 时运行。
我当然可以手动设置它,但我希望它作为我的自动化脚本的一部分。

这是我迄今为止用于计划任务的代码,我需要替换$Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily部分:

        Copy-Item "\\networkDrive\Backups\scripts\Reset-Sessions.ps1" "c:\scripts\Reset-Sessions.ps1"
        $Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily
        $User= 'Nt Authority\System'
        $Action= New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-executionpolicy bypass -File c:\scripts\Reset-Sessions.ps1"
        Register-ScheduledTask -TaskName "Reset-Sessions" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
Run Code Online (Sandbox Code Playgroud)

为了在线目的,我更改了一些目录和文件名。

如果有人可以引导我走向正确的方向或帮助举例,我将不胜感激。
我宁愿只更改 $Trigger 部分而不重写整个脚本,但如果不可能的话我会理解。

我使用 Powershell 版本 5.1。

powershell powershell-5.0

3
推荐指数
1
解决办法
1万
查看次数

如何从 Switch 语句中退出 While 循环

在 PowerShell 中,如何退出嵌套在语句while内的循环switch,而不执行紧随该块之后的代码while?我似乎无法弄清楚。到目前为止我所尝试的一切都会导致该代码块被执行。

这就是我想要实现的目标:

  1. 检查文件是否存在并在检测到文件时通知用户。
  2. 每10秒检查一次并通知用户
  3. 如果没有检测到文件,则退出循环并切换,然后继续步骤#2
  4. 如果 30 秒后仍检测到该文件,则超时并完全退出脚本。

这是代码:

try {
    #Step 1
    $Prompt = <Some Notification Dialog with two buttons>
    switch ($Prompt){
        'YES' {
            # Display the Windows Control Panel

            #Wait for user to manually uninstall an application - which removes a file from the path we will check later.
            $Timeout = New-Timespan -Seconds 30
            $Stopwatch = [Dispatch.Stopwatch]::StartNew()

            while ($Stopwatch.elapsed -lt $Timeout) {
                if (Test-Path -Path "C:\SomeFile.exe" -PathType Leaf) {
                    Write-Host …
Run Code Online (Sandbox Code Playgroud)

powershell scripting

3
推荐指数
1
解决办法
1859
查看次数

使用 Visual Studio Code 比较两个字符串?

Visual Studio Code 可用于比较文件。

"%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" --diff file1.cs file2.cs
Run Code Online (Sandbox Code Playgroud)

但是,是否可以使用它来比较存储在 PowerShell 变量中的两个文本?

$s1 = "abc
cde"
$s2 = "abc
xyz"
& "%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" --diff ....?
Run Code Online (Sandbox Code Playgroud)

Compare-Object如果两条文本不同,该cmdlet 仅显示它们。当文本很大时,它真的没什么用。

powershell visual-studio-code

3
推荐指数
1
解决办法
3807
查看次数

Powershell无法运行挂载

尝试将 NFS 共享挂载到 Windows 2012 R2 服务器上,但不确定如何解释抛出的错误。

以管理员身份运行 powershell 并输入以下命令...

PS C:\Windows\system32> whoami
domain\myuser


PS C:\Windows\system32> mount -o nolock mapr006:/mapr z:
New-PSDrive : Parameter cannot be processed because the parameter name 'o' is ambiguous. Possible matches include:
-OutVariable -OutBuffer.
At line:1 char:7
+ mount -o nolock mapr006:/mapr z:
+       ~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.NewPSDriveCommand



PS C:\Windows\system32> mount mapr006:/mapr z:

cmdlet New-PSDrive at command pipeline position 1
Supply values for the following parameters:
Root: …
Run Code Online (Sandbox Code Playgroud)

powershell windows-server-2012-r2

3
推荐指数
1
解决办法
1万
查看次数

无法捕获 NoProcessFoundForGivenName

我有一个 powershell 脚本,可以关闭 MS Word 中打开的文件名与模式匹配的所有 MS Word 实例。有时会调用 Powershell 脚本,但没有运行 MS Word 实例。cmdlet Get-Process 用于查找 MS Word 并检查当前打开的文件的名称。

无论是 [NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand] 还是
[Microsoft.PowerShell.Commands.GetProcessCommand] 都被捕获。

{
   $MSWordProcessHandle = Get-Process WINWORD
} catch [NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand] {
   Write-Output "No instances of MS Word found running."
}
Run Code Online (Sandbox Code Playgroud)

我不断收到下面的错误。

Get-Process : Cannot find a process with the name "WINWORD". Verify the process name and call the cmdlet again.  
At C:\Utils\scripts\Dev_Set_Close.ps1:24 char:27  
+    $MSWordProcessHandle = Get-Process WINWORD  
+                           ~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : ObjectNotFound: (WINWORD:String) [Get-Process], ProcessCommandException  
    + …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
1502
查看次数

获取计划任务以查看比上次运行更多的历史记录

当我跑步时

Get-ScheduledTask -TaskName "ttasskkk" | Get-ScheduledTaskInfo
Run Code Online (Sandbox Code Playgroud)

我仅收到有关上次运行时间的信息。如何使用 PS 查看最近 7 次或更多次运行的结果?

windows powershell

3
推荐指数
1
解决办法
1万
查看次数

在 PowerShell 中更改错误消息语言

我正在尝试在 powershell 中以英文显示错误,以便我可以更轻松地在线搜索它们。

\n

当出现错误时,它会以法语显示,如下所示:

\n
PS C:\\Users\\Olivier\\lpthw> type nul > ex2.py\ntype : Impossible de trouver le chemin d'acc\xc3\xa8s \xc2\xab\xc2\xa0C:\\Users\\Olivier\\lpthw\\nul\xc2\xa0\xc2\xbb, car il n'existe pas.\nAu caract\xc3\xa8re Ligne:1 : 1\n+ type nul > ex2.py\n+ ~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ObjectNotFound: (C:\\Users\\Olivier\\lpthw\\nul:String) [Get-Content], ItemNotFoundException\n    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand\n
Run Code Online (Sandbox Code Playgroud)\n

windows powershell

3
推荐指数
1
解决办法
3163
查看次数

在 PowerShell 中附加文本文件

因此,我正在尝试编写一个快速脚本来帮助我儿子学习拼字比赛。我几乎一切正常,但我想跟踪他的结果,所以我尝试将输出写入文本文件。我只关心最近的结果,所以当他开始测试时我删除了现有的结果文件。

我正在为每个人的拼写列表做一个,在每个列表的末尾,我有:

write-host "Word $counter - You spelled $wordcorrectly!!" | Out-File $outputpath -Append
Run Code Online (Sandbox Code Playgroud)

或者

write-host "Word $counter - Sorry.  The correct spelling is $word." | Out-File $outputpath -Append
Run Code Online (Sandbox Code Playgroud)

最后,在浏览完所有列表后,我得到:

write-host "You answered $counter out of $WordCount correctly" | Out-File $outputpath -Append
Run Code Online (Sandbox Code Playgroud)

但是当我转到 $outputpath 文件时,它完全是空白的......猜猜我在这里犯了什么简单的错误?

powershell append output

3
推荐指数
1
解决办法
5184
查看次数