小编Dar*_*te1的帖子

如何在Windows事件日志中存储对象?

最近,我们为所有脚本添加了选项,以便在Windows事件日志中记录其消息.这对于短消息非常有用,但我们似乎无法找到以结构化方式保存事件的方法,以便以后可以使用它们创建对象.

可以存储多个对象属性的事件示例: 服务控制经理

如何使用PowerShell完成此操作?

我们已经尝试过如下所述,但没有运气:

Write-EventLog -LogName HCScripts -Source 'Test (Brecht)' -EventId 4 -Message "<Data Name=""MyKey1"">MyValue1</Data>"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这篇文章中还有其他选项描述,但我们似乎无法弄清楚如何正确地做到这一点.

阅读事件的方法是:

Function Get-WinEventDataHC {
    Param (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [System.Diagnostics.Eventing.Reader.EventLogRecord[]]$Event
    )

    Process {
        foreach ($E in $Event){
            $XML = [XML]$E.ToXml()

            # Some events use other nodes, like 'UserData' on Applocker events...
            $XMLData = $null
            if ($XMLData = @($XML.Event.EventData.Data)){
                For ($i=0; $i -lt $XMLData.count; $i++){
                    $Params = @{
                        InputObject       = $E
                        NotePropertyName  = $EventXML.Event.EventData.Data[$i].Name
                        NotePropertyValue = $EventXML.Event.EventData.Data[$i].’#text’
                    }
                    Add-Member @Params
                }
            }

            $E …
Run Code Online (Sandbox Code Playgroud)

powershell events logging

11
推荐指数
1
解决办法
1244
查看次数

PowerShell 引用 Start-Process ArgumentList

我正在尝试让应用程序tablacus explorer打开文件夹路径。这适用于以下格式:

$exe = 'S:\Tools\explorer\TE64.exe'
Start-Process $exe -ArgumentList '"Tabs,Close other tabs" "Open,C:\Program Files"'
Run Code Online (Sandbox Code Playgroud)

但我真的很想在变量 ( $dir = 'C:\Program Files') 中包含路径,但我似乎无法正确获取引号,因此无法正确解释它。

quotes powershell

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

未捕获的错误:[vue-composition-api] 必须在使用任何函数之前调用 Vue.use(plugin)

考虑以下组合函数:

import { computed, ref } from '@vue/composition-api'
import { isInternetExplorer } from 'src/services/utils/utilsService'
import { Screen } from 'quasar'
import { auth, getAllScopes } from 'src/services/auth/authService'

const isLoginPopup = Screen.lt.sm || isInternetExplorer ? false : true
const accountID = ref('')

export const setAccountID = () => {
  const account = auth.getAccount()

  if (account) { accountID.value = account.idTokenClaims.oid } 
  else { accountID.value = '' }
}

export const useAccount = () => {
  const loading = ref(false)
  const disabled = …
Run Code Online (Sandbox Code Playgroud)

vue.js vuejs3 vue-composition-api

9
推荐指数
1
解决办法
4785
查看次数

TypeOrm 保存方法不更新对象

考虑这个实体:

// account.ts
import { Entity, PrimaryGeneratedColumn, PrimaryColumn, Column, BaseEntity, Index, CreateDateColumn,  UpdateDateColumn, OneToOne, JoinColumn } from 'typeorm'

@Entity()
export class Account extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ length: 50, unique: true })
  @Index({ unique: true })
  accountIdentifier: string

  @Column({ nullable: true, length: 100 })
  name?: string
}
Run Code Online (Sandbox Code Playgroud)

要保存新的account或返回现有的,account我们有以下工作代码:

const knownAccount = await Account.findOne({ accountIdentifier: token.oid })
if (knownAccount) return done(null, knownAccount, token)

const account = new Account()
account.accountIdentifier = token.oid
account.name = token.name
account.userName …
Run Code Online (Sandbox Code Playgroud)

javascript orm typeorm

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

PowerShell更改文件和文件夹的所有者

在网上搜索,我找到了2个能够更改文件和文件夹所有者的脚本.测试时,它在PowerShell 1.0中完美运行.现在我正在尝试将两者结合起来,以便它们以递归方式工作,因为我们的文件夹中包含超过500个子目录和文件.这是一项艰巨的任务......

我们想:

  • 在"\ server\C $\Folder"上运行一个脚本(不使用外部工具)
  • 将所有文件和子文件夹的所有者更改为"BUILTIN\Administrators"

问题:

  • 每个脚本仅适用于1个文件或1个文件夹.如何将它组合在一个脚本中,以便将所有子文件和文件组合在一起?把它放在2个不同的功能中可能并循环通过它或..

Script1:将FILE所有者更改为Admin

$File = "\\server\c$\Users\dir\Downloads\Target\TargetFile.txt"
$Account = New-Object System.Security.Principal.NTAccount("BUILTIN\Administrators")
$FileSecurity = new-object System.Security.AccessControl.FileSecurity
$FileSecurity.SetOwner($Account)
[System.IO.File]::SetAccessControl($File, $FileSecurity)
Run Code Online (Sandbox Code Playgroud)

Script2:将FOLDER所有者更改为Admin

$AdjustTokenPrivileges = @"
using System;
using System.Runtime.InteropServices;

 public class TokenManipulator
 {
  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
  ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  [DllImport("kernel32.dll", ExactSpelling = true)]
  internal static extern IntPtr GetCurrentProcess();
  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  internal static extern bool …
Run Code Online (Sandbox Code Playgroud)

powershell

6
推荐指数
2
解决办法
4万
查看次数

并非显示所有属性

当我们尝试通过管道将数据导出到其他函数时,我们在PowerShell中观察到一些奇怪的行为.

示例代码:

$Array = @()

$Obj1 = [PSCustomObject]@{
    Member1   = 'First'
    Member2   = 'Second'
}

$Obj2 = [PSCustomObject]@{
    Member1   = 'First'
    Member2   = 'Second'
    Member3   = 'Third'
}

$Array = $Obj1, $Obj2
$Array | Out-GridView -Title 'Not showing Member3'

$Array = $Obj2, $Obj1
$Array | Out-GridView -Title 'All members correctly displayed'
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,您可以看到当第一个对象仅包含2时properties,Out-GridViewCmdLet(和其他)仅显示2 properties,即使第二个对象具有3 properties.但是,当数组中的第一个对象有3时,properties它会正确地显示它们.

有没有解决的办法?因为不可能预先properties确定一个物体上有多少物体,并且物体中最多的物体properties将是第一个物体array.

arrays powershell properties

6
推荐指数
1
解决办法
618
查看次数

如何在Pester中模拟一份工作?

我们正试图评估是否Invoke-Command已经被称为一次.

Script.ps1

Get-Job | Remove-Job

Invoke-Command -ScriptBlock {'test'} -ComputerName localhost -AsJob

Wait-Job
Get-Job | Reveive-Job
Run Code Online (Sandbox Code Playgroud)

Script.Tests.ps1

Describe 'Test' {
    Mock Invoke-Command {
         # Mock here
    }
    It 'should be green' {
        ."$here\$sut" @Params
        Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是在Pester中模拟工作对象.当作业未被模拟时,Wait-Job将抛出一个未收到作业对象的错误.

如何模拟PowerShell作业对象Pester

powershell mocking pester

6
推荐指数
1
解决办法
350
查看次数

使用Pester测试强制性参数

我试图弄清楚如何对丢失的参数进行Pester测试:

Find-Waldo.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe 'Mandatory paramters' {
    it  'ComputerName' {
        {
            $Params = @{
                #ComputerName = 'MyPc'
                ScriptName   = 'Test'
            }
            . "$here\$sut" @Params
        } | Should throw
    }
}
Run Code Online (Sandbox Code Playgroud)

Find-Waldo.ps1

Param (
    [Parameter(Mandatory)]
    [String]$ComputerName,
    [String]$ScriptName
)

Function Find-Waldo {
    [CmdletBinding()]
    Param (
        [String]$FilePath
    )

    'Do something'
}
Run Code Online (Sandbox Code Playgroud)

每次我尝试assert结果或仅运行测试时,都会提示我输入ComputerName参数,而不是使测试失败。

我在这里想念什么超级明显吗?有没有办法测试强制性参数的存在?

powershell pester

5
推荐指数
1
解决办法
936
查看次数

Gulp 抛出错误“必须使用 import 来加载 ES 模块”

我们将gulpfile.jsin 节点转换v13.8.0为 ES6,如下所示:

import { src, dest, series, parallel, watch } from 'gulp'
import nodemon from 'gulp-nodemon' // normal nodemon does not display an error on app crash
import env from 'gulp-env'
import browser from 'browser-sync'
import sass from 'gulp-sass'
// Tasks
export default {
    cssTranspile: cssTranspile,
    jsTranspile: jsTranspile,
    server: series(startNodemon, startBrowserSync),
    default: series(
        parallel(
            cssTranspile,
            jsTranspile
        ),
        startNodemon,
        startBrowserSync,
        function () {
            watch('public/scss/*.scss', cssTranspile)
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

简单运行时报告的错误gulp是:

internal/modules/cjs/loader.js:1160
      throw new ERR_REQUIRE_ESM(filename, parentPath, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js gulp

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

为什么管道不适用于转换后的 json 对象?

考虑以下代码:

$data = '[
    {
        "Name":  "banana",
        "Color":  "yellow"
    },
    {
        "Name":  "kiwi",
        "Color":  "green"
    },
    {
        "Name":  "apple",
        "Color":  "red"
    }
]'
# Returns 3 objects while only 1 was expected
$data | ConvertFrom-Json | Where-Object { $_.Name -eq 'banana' }

# Workaround, returns 1 object as expected:
($data | ConvertFrom-Json) | Where-Object { $_.Name -eq 'banana' }
Run Code Online (Sandbox Code Playgroud)

为什么不能使用第一个选项?Where-Object从 json 转换对象后,该函数似乎不正确。这发生在 PowerShell 版本上5.1

我们在这里遗漏了一些明显的东西吗?

powershell powershell-5.1

5
推荐指数
1
解决办法
265
查看次数