powershell-2.0

Powershell logging from invoke-command

☆樱花仙子☆ 提交于 2019-12-05 17:25:33
I have a script that includes a function for logging. The function writes log to $msg variable then writes the message to the screen. i can redirect this output to a .txt file for logging. I need to run the script to multiple servers using invoke-command and write the log output to a txt file on the computer running the script. I am trying to figure out how to output $msg in the write-log function from the PSSession and return it so I can create a master log file for all the servers. Should I create and populate a custom object? Not sure how to do that and get the results back from the remote

ERROR: Description = Invalid query

允我心安 提交于 2019-12-05 16:53:24
Because of a messed up group policy object, multiple computers have TightVNC installed when they shouldn't. The GPO is gone, so just removing the software from there isn't an option that I'm aware of. Therefore, I'm scripting things in order to remove PowerShell from a list of computers. This is my script: if ($args.length -ne 1) { Write-Warning "Must pass computer name, ending script."; break } $pc = $args[0] Write-Output "Scanning $pc for TightVNC...." $prod = wmic /node:$pc product get name | where {$_ -match "TightVNC"} if ($prod) { Write-Output "Found TightVNC, attempting uninstall...."

PowerShell command to replace a chunk of text in a file

一个人想着一个人 提交于 2019-12-05 16:36:34
I am trying to replace a chunk of text in a file using PowerShell. For instance, I have a .sql file and I know the exact chunk of an SQL script that needs to be replaced in that particular file. After reading through some of the PowerShell replace examples it looks like PowerShell returns the content of the file in an array (each line representing one entry in the array). For example: GO :on error exit GO IF (DB_ID(N'$(DatabaseName)') IS NOT NULL) BEGIN ALTER DATABASE [$(DatabaseName)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [$(DatabaseName)]; END GO PRINT N'Creating $

Powershell search matching string in word document

半城伤御伤魂 提交于 2019-12-05 15:43:13
I have a simple requirement. I need to search a string in Word document and as result I need to get matching line / some words around in document. So far, I could successfully search a string in folder containing Word documents but it returns True / False based on whether it could find search string or not. #ERROR REPORTING ALL Set-StrictMode -Version latest $path = "c:\MORLAB" $files = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) } $output = "c:\wordfiletry.txt" $application = New-Object -comobject word.application $application.visible = $False

Why is Powershell ISE showing errors that Powershell console does not show?

会有一股神秘感。 提交于 2019-12-05 13:22:36
问题 I'm running exactly the same script.ps1 file in a Powershell ISE (manually loading the script and pressing F5) and in a Powershell console (executing the script file). They both work but ISE shows errors that the console does not. Why? The code is: git push origin master Write-Host "lastExitCode: $lastExitCode Last command was successful: $?" This code output this error in the ISE: git.cmd : Initializing to normal mode At E:\script.ps1:28 char:4 + git <<<< push origin master + CategoryInfo :

How can I append files using export-csv for PowerShell 2?

最后都变了- 提交于 2019-12-05 12:42:39
$filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation I've also tried $filesremoved | export-csv -Path E:\Code\powershell\logs\filesremoved.txt -NoTypeInformation -NoClobber The file appears to be overwritten every time. Is there a way to keep adding content to the file? I get errors Export-Csv : A parameter cannot be found that matches parameter name 'Append'. The -Append parameter of Export-Csv doesn't exist until PowerShell 3.0. One way to work around it in PowerShell 2.0 is to import the existing CSV, create some new rows, append the two collections,

PowerShell script to move files and folders including subfolders from one location to another older than x days

痞子三分冷 提交于 2019-12-05 12:03:59
问题 I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination. get-childitem -Path "\\servername\location" | where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | move-item -destination "C:\Dumps" I am unable to customize the script further. 回答1: Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the

How to access NoteProperties on the InputObject to a remoting session

六眼飞鱼酱① 提交于 2019-12-05 11:27:29
I'm having difficulty accessing PSObject s with NoteProperties defined, that are being passed through to a remote session via Invoke-Command . The reason I am constructing an object for this purpose is that I need access to two separate pieces of information in the remote session, but PowerShell only provides for a single InputObject . My code looks something like this: $sessionInput = new-object -TypeName System.Object $sessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty1 -Value @("some", "value") $sessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty2 -Value

How can I convert a hastable to a json string in powershell?

爱⌒轻易说出口 提交于 2019-12-05 10:51:31
I'm trying to convert a hashtable to a json object for use in a web service with powershell 2.0. $testhash = @{ Name = 'John Doe' Age = 10 Amount = 10.1 MixedItems = (1,2,3,"a") NestedHash = @{ nestedkey = "nextedvalue" } } function toJson($obj){ $ms = New-Object IO.MemoryStream $type = $obj.getType() [Type[]]$types = ($obj | select -expand PsTypeNames | Select -unique) + [type]'System.Management.Automation.PSObject' $js = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $type, $types, ([int]::MaxValue), $false, $null, $false $js.writeObject($ms, $obj) | out-null $utf8

Use the Get-Help cmdlet to display comment-based help in the same format

不问归期 提交于 2019-12-05 10:11:18
问题 I am trying to use the Get-Help cmdlet to display comment-based help in the same format in which it displays the cmdlet help topics that are generated from XML files. The ability to do this is documented in about_Comment_based_Help on TechNet, but when I execute the get-help cmdlet against my script I only get the script name returned. Any help would be appreciated! PS C:\Admin> Get-Help .\checksystem.ps1 -full checksystem.ps1 checksystem.ps1 script: function IsAlive { <# .DESCRIPTION Checks