Powershell 脚本仅在存在断点时才有效

戴夫·普拉特

我有一个 powershell 脚本,它读取文件和文件夹信息并将数据添加到数组中。

如果存在断点,脚本运行良好。断点可以在脚本中的任何位置,一旦到达并且我继续执行,代码就会毫无错误地完成。

即使我断点设置为DisabledGet-PSBreakpoint | Disable-PSBreakpoint行断点之前的代码执行没有错误。

当断点被删除时,代码错误“方法调用失败,因为[System.ManagementAutomation.PSObject]不包含op_Addition添加到数组时命名的方法

错误发生在这里

$report += New-Object -TypeName PSObject -Property $props

和这里

$filerec += New-Object -TypeName PSObject -Property $propsfile

这是发生错误的函数

    function ScanDrive([string]$scanpath)
{
    #Scan the drive
    $files = Get-ChildItem -Path $scanpath -Force -Recurse
    $filecount = $files.Count
    $Counter = 0

    #Set up the result array
    $folderRes = @()
    $filesRes = @()

    ForEach ($file in $files)
    {
        $lblInfo.Text = $file.FullName
        $Form.Refresh()
        $Counter++
        [Int]$Percentage = ($Counter/$filecount)*100
        $pbScan.Value = $Percentage
        Write-Debug "Help"
        if ($file.Attributes -eq 'Directory')
        {
            write-host 'This is a folder' + $file.FullName -ForegroundColor DarkGreen
            try
            {

                $acl = Get-Acl -path $file.PSPath
                Write-Host 'ACL' + $acl.Group.ToString()
                $access = $acl.access
                foreach($ar in $access)
                {
                    $props = [ordered]@{
                        'ID' = ''
                        'FolderName' = $file.Name;
                        'ADGroupUser' = $ar.IdentityReference;
                        'Permissions' = $ar.FileSystemRights.ToString();
                        'ControlType' = $ar.AccessControlType;
                        'IsInherited' = $ar.IsInherited
                        }
                    $report += New-Object -TypeName PSObject -Property $props 

                    $folderRes += $report
                }
                Write-Host "Folder record count : " + $folderRes.Count.ToString()
            }
            catch
            {
                Write-Host '******************************' -ForegroundColor Yellow
                Write-Host 'Error ' + $_.Exception.Message  -ForegroundColor Red
                Write-Host '******************************' -ForegroundColor Yellow
            }
        }
        else
        {
            write-host 'This is a file' + write-host $file.FullName -ForegroundColor DarkGreen

            $iname = $file.Name
            $iparent = $file.PSParentPath
            $isize = $file.Length
            $itype = $file.Extension
            $iowner = get-acl $file.FullName
            $owner = $iowner.Owner
            $icreated = $file.CreationTime
            $ilasmod = $file.LastWriteTime
            $idirectory = $file.Directory
            $ireadonly = $file.IsReadOnly
            $ilastacc = $file.LastAccessTime
            $ifilename = $file.FullName
            $usergroup = $iowner.Group
            $iatts = $file.Attributes

            $propsfile = [ordered]@{
                        'ID' = ''
                        'Name' = $iname;
                        'Parent' = $iparent;
                        'Size' = $isize;
                        'Type' = $itype;
                        'Owner' = $owner;
                        'CreatedDate' = $icreated;
                        'LastModDate' = $ilasmod;
                        'Directory' = $idirectory;
                        'IsReadOnly' = $ireadonly;
                        'LastAccessedDate' = $ilastacc;
                        'FileName' = $ifilename;
                        'UserGroup' = $usergroup;
                        'Attributes' = $iatts;
                        'LoggedDate' = Get-Date
                        }

                    $filerec += New-Object -TypeName PSObject -Property $propsfile 

                    $filesRes += $filerec
                }
    }

    Write-Host "File record count : " + $filesRes.Count.ToString()

    Return $filesRes,$folderRes

}
罗伯特·迪亚斯

您不需要添加到$filerecand $report,您可以覆盖它们(更改+==):

$filerec = New-Object -TypeName PSObject -Property $propsfile
$report = New-Object -TypeName PSObject -Property $props

当前代码尝试创建一个对象数组,然后将其添加到另一个数组中,该数组会在$folderRes和 中创建重复项$filesRes

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Active Directory Powershell 脚本仅在 Excel 中打开源 csv 后才有效

来自分类Dev

媒体查询断点仅在启用触摸模拟时才有效

来自分类Dev

仅在插入断点时才有效的 iOS Drawer UI

来自分类Dev

仅在Perl中存在哈希条目时才有效地获取它

来自分类Dev

Java脚本代码仅在将其放在正文部分时才有效

来自分类Dev

Java脚本代码仅在将其放在正文部分时才有效

来自分类Dev

Powershell表单按钮仅在最后一个选项卡上有效

来自分类Dev

__doPostBack仅在页面上有LinkButton,Calendar或WizardStep控件时才有效

来自分类Dev

Mypy:具有某些方法的通用容器仅在应用附加协议时才有效

来自分类Dev

CSS文本过渡仅在HTML输入具有“ required”标签时才有效

来自分类Dev

push.default 仅在没有定义 ref 规范时才有效?

来自分类Dev

如何验证该字段大于仅在检查特定按钮时才有效

来自分类Dev

SVG过滤器仅在样式属性中添加时才有效(Firefox)

来自分类Dev

glDrawArrays仅在我向其传递“错误”数据时才有效

来自分类Dev

页面上的自动滚动仅在 ctrl+单击使用 jquery 的按钮时才有效

来自分类Dev

数据绑定仅在 select ng-options 中使用 as 语法时才有效

来自分类Dev

wpf datagrid mousebinding leftdoubleclick 命令仅在项目已被选中时才有效

来自分类Dev

TWebBrowser - 它仅在显示其 Delphi 父窗体时才有效吗?

来自分类Dev

代码仅在未包装在函数中时才有效

来自分类Dev

为什么我的 tkinter 窗口仅在全局创建时才有效?

来自分类Dev

Nginx 仅在将工作进程用户设置为 root 时才有效

来自分类Dev

我的底部导航栏仅在我双击以切换片段时才有效

来自分类Dev

Powershell:无法使用其他有效脚本将其发布到Slack

来自分类Dev

仅当我在chrome调试器中附加了断点时,验证时间输入才有效

来自分类Dev

Windows 10快捷键仅在桌面上有快捷方式时才有效

来自分类Dev

脚本仅在Powershell中运行

来自分类Dev

Powershell脚本

来自分类Dev

Xcode(Swift)文档说将属性设置为YES,但仅在设置为true时才有效?

来自分类Dev

在C ++ Windows应用程序中引用本机C ++ DLL仅在作为引用添加时才有效

Related 相关文章

  1. 1

    Active Directory Powershell 脚本仅在 Excel 中打开源 csv 后才有效

  2. 2

    媒体查询断点仅在启用触摸模拟时才有效

  3. 3

    仅在插入断点时才有效的 iOS Drawer UI

  4. 4

    仅在Perl中存在哈希条目时才有效地获取它

  5. 5

    Java脚本代码仅在将其放在正文部分时才有效

  6. 6

    Java脚本代码仅在将其放在正文部分时才有效

  7. 7

    Powershell表单按钮仅在最后一个选项卡上有效

  8. 8

    __doPostBack仅在页面上有LinkButton,Calendar或WizardStep控件时才有效

  9. 9

    Mypy:具有某些方法的通用容器仅在应用附加协议时才有效

  10. 10

    CSS文本过渡仅在HTML输入具有“ required”标签时才有效

  11. 11

    push.default 仅在没有定义 ref 规范时才有效?

  12. 12

    如何验证该字段大于仅在检查特定按钮时才有效

  13. 13

    SVG过滤器仅在样式属性中添加时才有效(Firefox)

  14. 14

    glDrawArrays仅在我向其传递“错误”数据时才有效

  15. 15

    页面上的自动滚动仅在 ctrl+单击使用 jquery 的按钮时才有效

  16. 16

    数据绑定仅在 select ng-options 中使用 as 语法时才有效

  17. 17

    wpf datagrid mousebinding leftdoubleclick 命令仅在项目已被选中时才有效

  18. 18

    TWebBrowser - 它仅在显示其 Delphi 父窗体时才有效吗?

  19. 19

    代码仅在未包装在函数中时才有效

  20. 20

    为什么我的 tkinter 窗口仅在全局创建时才有效?

  21. 21

    Nginx 仅在将工作进程用户设置为 root 时才有效

  22. 22

    我的底部导航栏仅在我双击以切换片段时才有效

  23. 23

    Powershell:无法使用其他有效脚本将其发布到Slack

  24. 24

    仅当我在chrome调试器中附加了断点时,验证时间输入才有效

  25. 25

    Windows 10快捷键仅在桌面上有快捷方式时才有效

  26. 26

    脚本仅在Powershell中运行

  27. 27

    Powershell脚本

  28. 28

    Xcode(Swift)文档说将属性设置为YES,但仅在设置为true时才有效?

  29. 29

    在C ++ Windows应用程序中引用本机C ++ DLL仅在作为引用添加时才有效

热门标签

归档