Cannot create datastore Starwind from powercli - powercli

In vmware 6.7 i want create starwind datastore (iSCSI) with powercli. I tried some script like this:
Get-VMHost 192.168.10.210 | get-scsilun | where vendor -like "Starwind"
$SCSIID = Get-VMHost 192.168.10.210 | get-scsilun | where vendor -Like "Starwind"
$SCSIID = $SCSIID.CanonicalName
New-Datastore -VMHost 192.168.10.210 -Name "VolumeName" -Path $SCSIID
but i receive always the same error:
enter image description here
May you help me?

I would agree - most likely you have a problem with you $SCSIID variable. I have tested your code on my starwind setup and it works. Since you have only one device, you do not need to specify CanonicalName property. You can simply do:
Get-VMHost 192.168.10.210 | get-scsilun | where vendor -like "Starwind"
$SCSIID = Get-VMHost 192.168.10.210 | get-scsilun | where vendor -Like "Starwind"
New-Datastore -VMHost 192.168.10.210 -Name "VolumeName" -Path $SCSIID
Alternatively, you can simply filter block device by CanonicalName and create datastore. That's how I have done it:
$SCSIID = Get-VMHost 192.168.10.210 | get-scsilun | where -eq "eui.ca647c38f8e985f7"
New-Datastore -VMHost 192.168.10.210 -Name "VolumeName" -Path $SCSIID
FYI, I am posting StarWind related questions on their forum.
https://forums.starwindsoftware.com/

Related

How to pull SQL row with multiple values comma separated into PowerShell array to be used in Get-ChildItem -exclude

PowerShell newbie trying to figure out how to pull SQL row with multiple values comma separated into PowerShell array to be used in Get-ChildItem -exclude.
Please note I have the SQL portion working to where I can pull the values into PowerShell, but no matter what I have tried when I try to pass it to the -exclude option it thinks its just one name, versus comma separated names. I need help understanding how to pass the SQL row values into an array like this so it can be used properly with -exclude
View of hardcoded array that works
https://i.stack.imgur.com/9Fnsh.png
View of SQL table
https://i.stack.imgur.com/bNuKy.png
Code that gets it from SQL
https://i.stack.imgur.com/hCP2P.png
View of PS code to remove folders using the -ExcludeFolder option https://i.stack.imgur.com/dfvlK.png
$ExcludeFolder = $row.ExcludeFolder
Get-ChildItem -Path $Source -Recurse -Force -Exclude $excludeFolder | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null -and $_.LastWriteTime -lt $CutDay} | Remove-Item -Force -Recurse
Write-Host "End Time:" (Get-Date)
}
Thanks for any help you can provide.
SK
If you have a string in your $row.ExcludeFolders variable that reliably includes commas, you can use similar to the following to create an array:
You might then be able to pass the array as the -Exclude value

Powershell - using wildcards to search for filename

I am trying to make a PowerShell script that will search a folder for a filename that contains a certain file-mask. All files in the folder will have format like *yyyyMd*.txt.
I have made a script:
[String]$date = $(get-date -format yyyyMd)
$date1 = $date.ToString
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*$date1*'}
But this does not seem to work..
Can anyone help? It seems the problem is that the date variable is not correct because when I hard code something like below, it works:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*20141013*'}
You can simplify this by just using regex with the -match operator:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_ -match (Get-Date -format yyyyMMdd)}
And if you are on V3 or higher, you can further simplify to:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where Name -match (Get-Date -format yyyyMMdd)

Import-CSV Powershell, Exclude Records Containing String?

I have a section of a script here where I am importing a CSV but trying to only select records where AppName does not contain "Security"
Here is what I have but it doesn't seem to be working; the records I am trying to omit still appear.
$file2 = import-csv -Path "$UpdatePath\$($todaydate).csv" | where {$_.AppName -notcontains "Security"} | Select-Object AppName
Any suggestions greatly appreciated
Thanks
$file2 = import-csv -Path "$UpdatePath\$($todaydate).csv" | where {$_.AppName -notlike "*Security*"} | Select-Object AppName
-notlike will do this, but I also added asterisks which are a wildcard character in the like operators.
-contains is actually for arrays (think collections of values).
about_Comparison_Operators

Powershell get files older than x days and move them

I am sure this might have been asked a million times before. I am very new to Power Shell and would like to ask if I am doing this right.
In the directory, we have many files types. What I am trying to accomplish is to move only PDF files that are older than one month. To not even touch the other file extensions. The extensions in the folder are:
pdf, xml, csv
I have searched the forums prior to asking. This is what I have so far.
get-childitem -path \\server\folder -include "*.pdf" -exclude "*.xml,*.csv" | where-object {$_.LastWriteTime -gt (get-date).AddDays(-31)} | move-item -destination \\server\folder\folder2
One question though, how would you handle an exclude if there is no file extension?
Thanks for your time and patience with this noob!
There is no need to use "where" to test the extension as get-childitem does this for you. Although I would use the filter parameter (2nd positional parameter) in the case of a single extension to search for e.g.:
$date = (get-date).AddDays(-31)
get-childitem \\server\folder *.pdf | where-object {$_.LastWriteTime -gt $date} |
move-item -destination \\server\folder\folder2
Btw using the filter parameter is also faster which maybe important when searching a network share.
One way is to add $_.Extension -eq ".pdf" to your where-object block so that you only grab those extensions.
get-childitem -path \\server\folder | where-object {
$_.extension -eq ".pdf" -and ($_.LastWriteTime -gt (get-date).AddDays(-31))} | move-item -destination C:\test\test
Also, if you want files older than one month, your date comparison needs to be -lt and not -gt
get-childitem -path \\server\folder | where-object {
$_.extension -eq ".pdf" -and ($_.LastWriteTime -lt (get-date).AddDays(-31))} | move-item -destination C:\test\test
Another way to do this is to specify the -Name flag and use wildcard for filename:
get-childitem -path \\server\folder -name "*.pdf" | where-object {$_.LastWriteTime -gt (get-date).AddDays(-31)} | move-item -destination \\server\folder\folder2
I am not sure to understand your last question, but if you have pdf files with no extension, see this discussion.
I think something like that can help you. The file.exe (download) showing the mime-type of the files, which is something like PDF document, version 1.6, depending the pdf file version.
gci . | ? { (file.exe -b $_) -match "pdf" }

powershell replace "ID= a long number"

I am using a Find and Replace script found on StackOverflow.
Get-Content test.txt | `
ForEach-Object { $_ -replace "foo", "bar" } | `
Set-Content test2.txt
and as long as i am searching for foo and want to replace it with bar this works great.
My problem is that I want to replace ID="123456789" where the numbers are different every time. So the only thing i know for sure is that i want to search for "ID=" and the rest is variable. When i use my good friend to find a answer to this question i don't get anything because the word variable is in it.
The first argument of the -replace opeartor is a regex, so:
Get-Content test.txt |
ForEach-Object { $_ -replace "ID=\d+", "bar" } |
Set-Content test2.txt
Also, the -replace operator will work just as well on an array, so unless your file is really large you can also do this:
(Get-Content test.txt) -replace "ID=\d+", "bar" | Set-Content test2.txt