最近,我们为所有脚本添加了选项,以便在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) 我正在尝试让应用程序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'
) 中包含路径,但我似乎无法正确获取引号,因此无法正确解释它。
考虑以下组合函数:
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) 考虑这个实体:
// 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) 在网上搜索,我找到了2个能够更改文件和文件夹所有者的脚本.测试时,它在PowerShell 1.0中完美运行.现在我正在尝试将两者结合起来,以便它们以递归方式工作,因为我们的文件夹中包含超过500个子目录和文件.这是一项艰巨的任务......
我们想:
问题:
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中观察到一些奇怪的行为.
示例代码:
$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-GridView
CmdLet(和其他)仅显示2 properties
,即使第二个对象具有3 properties
.但是,当数组中的第一个对象有3时,properties
它会正确地显示它们.
有没有解决的办法?因为不可能预先properties
确定一个物体上有多少物体,并且物体中最多的物体properties
将是第一个物体array
.
我们正试图评估是否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
?
我试图弄清楚如何对丢失的参数进行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
参数,而不是使测试失败。
我在这里想念什么超级明显吗?有没有办法测试强制性参数的存在?
我们将gulpfile.js
in 节点转换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) 考虑以下代码:
$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 ×7
javascript ×2
pester ×2
arrays ×1
events ×1
gulp ×1
logging ×1
mocking ×1
node.js ×1
orm ×1
properties ×1
quotes ×1
typeorm ×1
vue.js ×1
vuejs3 ×1