I want to convert a powershell output into a html file - html

I wanted to convert a powershell result to html file,
I have an array $f="gsds,jv,hfvw".
I want the html file to print each element of the array in a new line.
How to do it?
Thanks!
I used this code
$f="6e47812,662348,8753478"
Get-Service |ConvertTo-Html -Body "$f"|out-file D:\service.html
And the out put was
6e47812,662348,8753478

Do you mean something like this? :
$test = "abc","def","ghi"
foreach ($row in $test) {Write-Output "<p> $row </p>"}
Output :
<p> abc </p>
<p> def </p>
<p> ghi </p>

cls
$Path = 'C:\temp\test' #Path Directory
Grab a recursive list of all subfolders
$SubFolders = dir $Path -Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object -Process {$_.FullName}
Iterate through the list of subfolders and grab the first file in each
ForEach ($Folder in $SubFolders)
{
$FullFileName = dir $Folder | Where-Object {!$_.PSIsContainer} | Sort-Object {$_.LastWriteTime} -Descending | Select-Object -First 1
For every file grab it's location and output the robocopy command ready for use
ForEach ($File in $FullFileName)
{
$FilePath = $File.DirectoryName
$FileName = $File.Name
write-output "$FilePath $FileName"
}
}

Related

Unable to Fetch PDF files from One Drive using PowerShell

I am trying to fetch pdf files from a OneDrive, However, the only pdf file is not coming up with my link so that I can share with others via email However I am able to get into the folder wherein my pdf files are stored.
Below is my code which I tried however I am not getting the pdf file.
$linklist = [System.Collections.ArrayList]#()
$prefix="https://abc.sharepoint.com/:b:/r/personal/svc_prod_bcs_user_abc_com/Documents/ads/SETF/"
$a = Get-Content '.\New document 2.json' | ConvertFrom-Json
$a | ForEach-Object {
$TestEvent = #($a.TestEvent)
$Folder = #($a.Folder)
$NEL = #($a.NEL)
for ($i = 0; $i -lt $NEL.Count; $i++) {
$path = 'D:\\OneDriveData\\OneDrive - abc Enterprises\\ads\\SETF\\'+$Folder[$i]+'\\'
$pdf = Get-ChildItem -Path $path -Filter "$TestEvent[$i]" | Sort {$_.LastWriteTime} | select -last 1
$url1 = ''+$Folder[$i]+''+'<br>'+'<br>'
write-host $url1
$null = $linklist.Add($url1)
}
}
"LINKS_VAR=$linklist" | Out-File $ENV:JOB_BASE_NAME'.properties' -Encoding ASCII

Unable to combine all csv files using powershell

I would like to combine all the csv files in my local folder but it shows empty results. I am trying to take the header of the first file and skip all the headers in the rest of the files in the folder and join them.
get-childItem "C:\Users\*.csv" | foreach {[System.IO.File]::AppendAllText
("C:\Users\finalCSV.csv", [System.IO.File]::ReadAllText($_.FullName))}
$getFirstLine = $true
get-childItem "C:\Users\*.csv" | foreach {
$filePath = $_
$lines = $lines = Get-Content $filePath
$linesToWrite = switch($getFirstLine) {
$true {$lines}
$false {$lines | Select -Skip 1}
}
$getFirstLine = $false
Add-Content "C:\Users\finalCSV.csv" $linesToWrite
}
My end result is that when I open finalCSV.csv it shows no results.
I think you are trying to overwork your solution. Just use Import-Csv and append to an array. Something like this:
$a = #(); ls *.csv | % {$a += (Import-Csv $_.FullName)}; $a
Works even if the columns are in a different order.

Powershell - Delete all lines in a CSV up to and including a specific string for all files in a folder.

I have the following code which searches for a string in a file and returns the line number to a variable. It then deletes all lines up to and including this line. I need to modify this code to run for all .CSV files in a directory.
$a = Select-String draft.csv -Pattern RESULTS -CaseSensitive | Select -expand LineNumber
$content = Get-Content draft.csv
$content | Foreach {$n=1} {if ($n++ -ge ($a+1)) {$_}} > draft.csv
I was able to accomplish this task using the following. If anyone has a faster method, I'm glad to hear it.
ForEach ($file in Get-ChildItem *.csv)
{
$filename = $file.fullname
$a = Select-String $filename -Pattern RESULTS -CaseSensitive | Select -expand LineNumber
$content = Get-Content $filename
$content | Foreach {$n=1} {if ($n++ -ge ($a+1)) {$_}} > $filename
Echo $filename "is Complete.... Moving On!"
}

Concatenate multiple Variables/CSVs and export as one CSV file Powershell

For example,
I have CSV file and imported to powershell
$fullname = Import-Csv “fullname.csv”
$fullname
Output is
FullName
------------------
John Smith
Kevin Johnson
I have another CSV file and imported to
$Email = Import-Csv “Email.csv”
$Email
the output is
Email
-------
jhrjf#gmail.com
hheraf1010#gmail.com
I would like to concatenate this 2 variables and export to as one csv file, so I tried like this
$fullname = Import-Csv “fullname.csv”
$fullname
$Email = Import-Csv “Email.csv”
$Email
($fullname+$Email)|Export-Csv C:\fullnameandEmail.csv -NoTypeInformation
i also try like this
-join($fullname,$Email)|Export-Csv C:\fullnameandEmail.csv -NoTypeInformation
but it was not working,
I would like to make csv like below, how can I concatenate these 2 valuables?
FullName Email
--------- ----------
John Smith jhrjf#gmail.com
Kevin Johnson hheraf1010#gmail.com
Thank you so much
It's helpful to understand that the $fullname and $email objects you have in memory after importing a CSV are actually arrays of objects. Each object has one or more properties that represent the column values from the CSV.
You can loop through the objects in either of the arrays and use the Add-Member cmdlet to add a new property to each object.
The following code loops through the $email array and for each item, it adds a property with the Email value to the corresponding item in the $fullname array. It then exports that merged array to a CSV file.
$fullname = Import-Csv "fullname.csv"
$email = Import-Csv "Email.csv"
$i = 0
$email | ForEach-Object {
Add-Member -inputobject $fullname[$i] -name Email -value $_.Email -membertype NoteProperty;
$i++}
$fullname | Export-Csv -notype -path "C:\fullnameandEmail.csv"
So, for simplicity I would combine what the other two have suggested. Use a For loop, and then within the loop use Add-Member.
$fullname = Import-Csv “fullname.csv”
$Email = Import-Csv “Email.csv”
For($i=0;$i -lt $fullname.count;$i++){
$FullName[$i] | Add-Member 'Email' $Email[$i].email
}
$FullName | Export-CSV -NoType Output.csv
$names = Import-Csv "fullname.csv"
$emails = Import-Csv "email.csv"
for ( $n = 0; $n -lt $names.Count; $n++ ) {
New-Object PSObject -Property #{
"FullName" = $names[$n].FullName
"Email" = $emails[$n].Email
} | Select-Object FullName,Email
}

Get AD distinguished name

I'm trying to take input from a CSV file, which has a list of group names (canonical names) and get the Distinguished Name from it, then output to another CSV file. The code:
#get input file if passed
Param($InputFile)
#Set global variable to null
$WasError = $null
#Prompt for file name if not already provided
If ($InputFile -eq $NULL) {
$InputFile = Read-Host "Enter the name of the input CSV file (file must have header of 'Group')"
}
#Import Active Directory module
Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue
$DistinguishedNames = Import-Csv -Path $InputFile -Header Group | foreach-Object {
$GN = $_.Group
$DN = Get-ADGroup -Identity $GN | Select DistinguishedName
}
$FileName = "RESULT_Get-DistinguishedNames" + ".csv"
#Export list to CSV
$DNarray | Export-Csv -Path $FileName -NoTypeInformation
I've tried multiple solutions, and none have seemed to work. Currently, it throws an error because
Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
I tried using -Filter also, and in a previous attempt I used this code:
Param($InputFile)
#Set global variable to null
$WasError = $null
#Prompt for file name if not already provided
If ($InputFile -eq $NULL) {
$InputFile = Read-Host "Enter the name of the input CSV file(file must have header of 'GroupName')"
}
#Import Active Directory module
Import-Module -Name ActiveDirectory -ErrorAction SilentlyContinue
$DistinguishedNames = Import-Csv -Path $InputFile | foreach {
$strFilter = "*"
$Root = [ADSI]"GC://$($objDomain.Name)"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$objSearcher.Filter = $strFilter
$objSearcher.PageSize = 1000
$objsearcher.PropertiesToLoad.Add("distinguishedname") | Out-Null
$objcolresults = $objsearcher.FindAll()
$objitem = $objcolresults.Properties
[string]$objDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
[string]$DN = $objitem.distinguishedname
[string]$GN = $objitem.groupname
#Get group info and add mgr ID and Display Name
$props = #{'Group Name'= $GN;'Domain' = $objDomain;'Distinguished Name' = $DN;}
$DNS = New-Object psobject -Property $props
}
$FileName = "RESULT_Get-DistinguishedNames" + ".csv"
#Export list to CSV
$DistinguishedNames | Sort Name | Export-Csv $FileName -NoTypeInformation
The filter isn't the same one I was using here, I can't find the one I was using, the I currently have is a broken attempt.
Anyway, the main issue I was having is that it will get the group name, but search for it in the wrong domain (it wouldn't include Organizational Units) which caused none of them to be found. When I search for a group in PowerShell though (using Get-ADGroup ADMIN) they show up with the correct DN and everything. Any hints or code samples are appreciated.
You seemingly miss the point of $variable = cmdlet|foreach {script-block} assignment. The objects to assign to $variable should be returned (passed through the script block) in order to end up in $variable. Both your main loops contain the structure of the line $somevar=expectedOutput where expectedOutput is either a New-Object psobject or Get-ADGroup call. The assignment to $someVar suppresses the output, so that the script block does not have anything to return, and $variable remains null. To fix, do not prepend the call that should return an object into outside variable with an assignment.
$DistinguishedNames = Import-Csv -Path $InputFile -Header Group | foreach-Object {
$GN = $_.Group
Get-ADGroup -Identity $GN | Select DistinguishedName # drop '$DN=`
}
$DistinguishedNames | Export-CSV -Path $FileName -NoTypeInformation
The same issue with the second script.