Output an array to HTML file - html

I have been given a working PowerShell script to modify. This script checks for broken links in a SharePoint web application and reports any broken links it finds. Currently, the script outputs the results to a text file.
Code:
$results | Out-File report.txt
Result Format:
Name of link list - ID of the item - URL + URL Name - HTTP status code
- URL
Result:
Link List 1 - 1 - http://google.com, Google (Good) - 200 -
http://google.com
However, when I try to use the ConvertTo-HTML function I get a different output:
Code:
$results | ConvertTo-Html | Out-File report.html
Result:
76
Therefore, instead of receiving a string of text I am receiving its length.
What am I doing wrong here?
Note:
results is an array.

Convertto-Html is not meant to be used like that, from help:
Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser.
You need to send an object (resulting from a command) to this cmdlet, not just a bunch of text. See: Get-Help Convertto-Html -Examples
this is a way to do what you want:
$results | ForEach-Object {Add-Member -InputObject $_ -Type NoteProperty -Name Value -Value $_; $_} | ConvertTo-Html -Property Value

Related

Special characters in AD to Json Powershell script

I have this PowerShell script:
Import-Module ActiveDirectory
Get-ADUser -filter 'SN -eq "Buyl"' -Properties UserPrincipalName,EmployeeNumber,Description,Department,GivenName,sn |
select #{N='UPN';E={$_.UserPrincipalName}},#{N='POINTER';E={$_.EmployeeNumber}},#{N='DESCRIPTION';E={$_.Description}},#{N='DEPARTMENT';E={$_.Department}},#{N='LASTNAME';E={$_.GivenName}},#{N='FIRSTNAME';E={$_.sn}},#{N='OU';E={$ou}} |
ConvertTo-Json
This works fine in PowerShell with users like Anaïs in their first name. I run this script from a PHP script that parses the Json output using json_decode. It seems the special characters break this function.
How can I make sure the PowerShell sends the output in clean UTF8 format that can be parsed with PHP?

ConvertFrom-Json output doesn't work for Select-Object

I am using PowerShell 4 to make a series of web requests. From one call I get a generic array - for the sake of discussion it looks like this
$data = '[{"Id":"1","Name":"One"},{"Id":"2","Name":"Two"}]'
I am trying to parse this data to pull out the Name properties. However, when I use the following call it writes a line with Name and no information under it:
$data | ConvertFrom-Json | Select-Object Name
But if I save the object to an intermediate object like this it works:
$o1 = $data | ConvertFrom-Json
$o1 | Select-Object Name
I get the proper output.
The object types are different but I don't understand why. Here's output from relevant Get-Member calls:
$test | ConvrtFrom-Json | gm
TypeName: System.Object[]
and
$o1 | gm
TypeName: System.Management.Automation.PSCustomObject
Can anyone help me understand what I'm doing wrong in terms of my collection management? I'd like to be able to do this in one statement.
It seems like the parentheses are needed for some mysterious reason, as OP pointed out in the comment. Adding parentheses was the fix for me as well.
I am not sure why this breaks without parens, but I can confirm that code without parens is not an issue when executed in powershell core.

Powershell file URL / file filtering

I am trying to generate an html page with file index. This approach worked seamlessly:
$htmlout = Get-ChildItem -Path "$SearchPath" -Filter "$fileType" -Recurse |
Select #{Name="Link";Expression={("<a rel=" + $_.FullName + " href=file:///" + $_.FullName + ">$_</a>")}}
The Link column had file names only (i.e. test.txt) and displayed file content when clicking on it. Then we've got an additional requirement to skip old files. The script is now:
$htmlout = Get-ChildItem -Path "$SearchPath" -Recurse -include ("$fileType") | Where-Object {$_.LastWriteTime -ge "01/01/2014"} |` Select #{Name="Link";Expression={("<a rel=" + $_.FullName + " href=file:///" + $_.FullName + ">$_</a>")}}
It still works, but Link column now displays the entire file path + file name (i.e. \fileserver\folder1\folder2\test.txt).
Adding >$_.Name< does not work here.
I am trying to understand why the same URL line behaves differently after filter change.
Background
As far as I can tell, there is a discrepancy with the interaction between the .ToString() method and the DefaultDisplayProperty of objects returned by Get-ChildItem.
The behavior manifests when both of the following conditions are true:
- The -filter parameter is being used.
- The value of the -Path parameter resolves to a single directory, whether or not -Recurse is used.
Under the above circumstances, the .ToString() method implemented by PowerShell uses the Name property as default, rather than FullName as is the case in all other scenarios.
My guess is that this inconsistency is due to the underlying object types returned by the FileSystem provider when -Filter is used, rather than the objects PowerShell returns when it handles the search/filter itself (as is the case with -Include).
Observation
When you wrap your $_ pipeline object variable in double quotes, PowerShell's type-conversion implicitly calls the .ToString() method and you get the resulting name variation.
Solution
To correct your issue, you could simply use -Filter in both code examples and get the desired output, however, that is prone to cause problems sooner or later.
The more appropriate way to negate the problem is to properly use a PowerShell sub-expression within the double-quoted strings.
To create a sub-expression, simply wrap the desired code like so: $(). This creates a separation between which characters are code and which are part of the string; in your case allowing you to use the member access operator .. The method also alleviates the need to do string concatenation with the + operator.
Solution Code:
$HTMLOut = Get-ChildItem -Path $SearchPath -Recurse -Include $FileType | Where-Object {$_.LastWriteTime -ge "01/01/2014"} | Select #{Name="Link";Expression={("<a rel=$($_.FullName) href=file:///$($_.FullName)>$($_.Name)</a>")}}

Can I assign Variables from selected Row using out-gridview -passthru

I'm very new to PowerShell and I'm trying to build on older batch files that I made into PowerShell and add some features.
At the moment I have a CSV file which I've used in the pass as a sort of "environment" file, previously I would do batch jobs against this CSV file.
I have a line
Import-Csv "csvfile" | select-object -property * | out-gridview -passthru
The CSV file is built something like:
Name,location,folder
Test,e,Testsite
Test1,c,windows
test2,c,temp
Basically I want to select one of the grows and click Okay and assign the 3 items to variables.. $foldername,$driveLetter,$destinationDirectory
I've looked high and low and I can't seem to manage it I did find one example on StackOverflow which I shamelessly copied, massaged and got to work ... but that gridview is prebuilt by the OP of that post and doesn't have things like the piping to grid-view.-Passthru has (Filter & scroll bar) but I was able to assign variables using this method but my CSV is pretty huge and I want to be able to have it auto size itself and filter / scroll.
You need to use the -OutPutMode Single option of Out-Gridview to restrict selection to a single item from the gridview.
Import-Csv "csvfile" |
select-object -property * |
out-gridview -OutputMode Single -Title 'Select a row' |
ForEach-Object {
$foldername,$driveLetter,$destinationDirectory = $_.Name,$_.location,$_.folder
}

Export the result to CSV Azure PowerShell Warnign Sign

I am assuming this warning is causing problem.
WARNING: GeoReplicationEnabled property will be deprecated in a future
release of Azure PowerShell. The value will be merged into the
AccountType property
because when I did this command
Get-AzureWebsite | export-csv -Path "C:\Users\km\Desktop\AzureProject\Hello Pay-As-You-Go-Website.csv"
my CSV file is totally fine
SO the problem I am having is
When I execute this command
Get-AzureStorageAccount | Format-Table -Property StorageAccountName, Location, AccountType, StorageAccountStatus
The result is like this
StorageAccountName Location AccountType
StorageAccountStatus
--------------------- --------- ------------ -------------------- HelloSushi East US Standard_GRS Created
WARNING: GeoReplicationEnabled property will be deprecated in a future
release of Azure PowerShell. The value will be merged into the
AccountType property.
and I add this code to move this result to CSV like this
Get-AzureStorageAccount | Format-Table -Property StorageAccountName, Location, AccountType, StorageAccountStatus | export-csv -Path "C:\Users\km\Desktop\AzureProject\Susco Pay-As-You-Go-Storage.csv"
but I checked on CSV.file, it is totally does not make sense. it is not same one.
so ,
I would like to show the result exactly on CSV like when I did this code
Get-AzureStorageAccount | Format-Table -Property StorageAccountName, Location, AccountType, StorageAccountStatus
How can I do that?
Try Out-File instead of Export-CSV it is giving the exact same output as console
==
You can't do a Format-List to Export-Csv, this link explains it.