how to remove curly braces and get proper output in csv fileHow to deal with System.String[]Why does continue behave like break in a Foreach-Object?WMI Export-CSV creates System.String for IP Addresses (dynamically selecting objects)How to pipe “member of” to csv in PowershellTrying to capture IP from powershell script, getting System.String[] insteadCSV output from PowerShell (test-connection) appearing blank when more than one line in source CSVExport-Csv help in PowerShellUse Import-Csv cmdlet with all files in directoryPowershell script to read 2 columns data from .csv filePowerShell null array error (string:Null Error)
How can I answer high-school writing prompts without sounding weird and fake?
Why does a C.D.F need to be right-continuous?
Help decide course of action for rotting windows
Make all the squares explode
Should these notes be played as a chord or one after another?
How do I get past a 3-year ban from overstay with VWP?
Increase height of laser cut design file for enclosure
Is a diamond sword feasible?
How are one-time password generators like Google Authenticator different from having two passwords?
How to pronounce "r" after a "g"?
LocalDate.plus Incorrect Answer
Why does the Earth follow an elliptical trajectory rather than a parabolic one?
expl3-strategy to automatically update the title of a document, depending on its content
Can 'sudo apt-get remove [write]' destroy my Ubuntu?
Control variables and other independent variables
Why did God specifically target the firstborn in the 10th plague (Exodus 12:29-36)?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
What happened to python's ~ when working with boolean?
The lexical root of the perfect tense forms differs from the lexical root of the infinitive form
Is there a faster way to calculate Abs[z]^2 numerically?
Washer drain pipe overflow
What does this quote in Small Gods refer to?
Was there ever any real use for a 6800-based Apple I?
International Code of Ethics for order of co-authors in research papers
how to remove curly braces and get proper output in csv file
How to deal with System.String[]Why does continue behave like break in a Foreach-Object?WMI Export-CSV creates System.String for IP Addresses (dynamically selecting objects)How to pipe “member of” to csv in PowershellTrying to capture IP from powershell script, getting System.String[] insteadCSV output from PowerShell (test-connection) appearing blank when more than one line in source CSVExport-Csv help in PowerShellUse Import-Csv cmdlet with all files in directoryPowershell script to read 2 columns data from .csv filePowerShell null array error (string:Null Error)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I m trying to get the list of vm from nutanix with name, ipaddress,
The output I am recieving includes ipaddress with curly braces which gives output as System.String[]
I have taken all the values in a array by using a for loop, than have exported the values to csv
Script which i have written is as follows-
foreach ($vmachine in $vm) Select "vmName", "ipAddresses", "description", "protectionDomainName", "powerState"
$obj.vmName = $vmachine.vmName
$obj.ipAddresses = $vmachine.ipAddresses
$obj.description = $vmachine.description
$obj.protectionDomainName = $vmachine.protectionDomainName
$obj.powerState = $vmachine.powerState
$outArrayVM += $obj
$obj =$null
$outArrayVM | Export-Csv d:z.csv
Expected output should be some ipaddress like 10.x.x.x, but m getting @ipAddresses=System.String[]
powershell
add a comment |
I m trying to get the list of vm from nutanix with name, ipaddress,
The output I am recieving includes ipaddress with curly braces which gives output as System.String[]
I have taken all the values in a array by using a for loop, than have exported the values to csv
Script which i have written is as follows-
foreach ($vmachine in $vm) Select "vmName", "ipAddresses", "description", "protectionDomainName", "powerState"
$obj.vmName = $vmachine.vmName
$obj.ipAddresses = $vmachine.ipAddresses
$obj.description = $vmachine.description
$obj.protectionDomainName = $vmachine.protectionDomainName
$obj.powerState = $vmachine.powerState
$outArrayVM += $obj
$obj =$null
$outArrayVM | Export-Csv d:z.csv
Expected output should be some ipaddress like 10.x.x.x, but m getting @ipAddresses=System.String[]
powershell
add a comment |
I m trying to get the list of vm from nutanix with name, ipaddress,
The output I am recieving includes ipaddress with curly braces which gives output as System.String[]
I have taken all the values in a array by using a for loop, than have exported the values to csv
Script which i have written is as follows-
foreach ($vmachine in $vm) Select "vmName", "ipAddresses", "description", "protectionDomainName", "powerState"
$obj.vmName = $vmachine.vmName
$obj.ipAddresses = $vmachine.ipAddresses
$obj.description = $vmachine.description
$obj.protectionDomainName = $vmachine.protectionDomainName
$obj.powerState = $vmachine.powerState
$outArrayVM += $obj
$obj =$null
$outArrayVM | Export-Csv d:z.csv
Expected output should be some ipaddress like 10.x.x.x, but m getting @ipAddresses=System.String[]
powershell
I m trying to get the list of vm from nutanix with name, ipaddress,
The output I am recieving includes ipaddress with curly braces which gives output as System.String[]
I have taken all the values in a array by using a for loop, than have exported the values to csv
Script which i have written is as follows-
foreach ($vmachine in $vm) Select "vmName", "ipAddresses", "description", "protectionDomainName", "powerState"
$obj.vmName = $vmachine.vmName
$obj.ipAddresses = $vmachine.ipAddresses
$obj.description = $vmachine.description
$obj.protectionDomainName = $vmachine.protectionDomainName
$obj.powerState = $vmachine.powerState
$outArrayVM += $obj
$obj =$null
$outArrayVM | Export-Csv d:z.csv
Expected output should be some ipaddress like 10.x.x.x, but m getting @ipAddresses=System.String[]
powershell
powershell
edited Mar 23 at 14:55
Theo
7,1393521
7,1393521
asked Mar 23 at 10:58
fmsrvfmsrv
336
336
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This happens because $vmachine.ipAddresses is a string array object. You want a string representation of that with controlled formatting. There are many ways to accomplish this. Here is one that will join multiple IPs (if they exist) using a ;. If there is only one IP, it will appear with no semi-colon:
$obj.ipAddresses = $vmachine.ipAddresses -join ";"
Here's an example of your scenario:
$ip = @("10.1.23.45")
$ip.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$obj.name = "test"
$obj.ip = $ip
$obj
name ip
---- --
test 10.1.23.45
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","System.Object[]"
Converting the ip property of $obj to string forces PowerShell to interpret the property as a string rather than a collection. Thus, the braces notation () goes away.
$obj.ip = $ip -join ";"
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","10.1.23.45"
Here are some other alternatives to set the ip property value as a string:
$obj.ip = -join $ip # No join character here. Works best with only one IP.
$obj.ip = $ip[0] # Accesses first element of array $ip, which will be a string. Only works with one IP.
$obj.ip = [string]$ip # Uses string type accelerator to cast $ip as string. This will join multiple IPs with a space between each IP.
Explanation:
When a ConvertTo-Csv or Export-Csv is run, the input object property is converted using the ToString() method. If the reference type of that object property (System.Array in this case) does not have an override for the ToString() method, then that method will return the fully qualified type name of the property. In this instance, that FQTN is System.Object[]. This is predictable with a little digging.
Testing with [Int32], you would expect the string conversion to provide a string representation of the integer data because it does have an override:
$int = 1
$int.gettype().fullname
System.Int32
($int | Get-Member).where$_.Name -eq "ToString"
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provid...
$int.ToString()
1
$int.ToString().gettype().fullname
System.String
Testing with [Array], you would not expect the string conversion to provide a string representation of the array data because it does not have an override:
$arr = [array]1
$arr.gettype().fullname
System.Object[]
([System.Object[]] | Get-Member -Static).where$_.name -eq "ToString"
$arr.toString()
System.Object[]
See Export-Csv and Object.ToString Method for supplemental explanations and examples.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312981%2fhow-to-remove-curly-braces-and-get-proper-output-in-csv-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This happens because $vmachine.ipAddresses is a string array object. You want a string representation of that with controlled formatting. There are many ways to accomplish this. Here is one that will join multiple IPs (if they exist) using a ;. If there is only one IP, it will appear with no semi-colon:
$obj.ipAddresses = $vmachine.ipAddresses -join ";"
Here's an example of your scenario:
$ip = @("10.1.23.45")
$ip.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$obj.name = "test"
$obj.ip = $ip
$obj
name ip
---- --
test 10.1.23.45
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","System.Object[]"
Converting the ip property of $obj to string forces PowerShell to interpret the property as a string rather than a collection. Thus, the braces notation () goes away.
$obj.ip = $ip -join ";"
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","10.1.23.45"
Here are some other alternatives to set the ip property value as a string:
$obj.ip = -join $ip # No join character here. Works best with only one IP.
$obj.ip = $ip[0] # Accesses first element of array $ip, which will be a string. Only works with one IP.
$obj.ip = [string]$ip # Uses string type accelerator to cast $ip as string. This will join multiple IPs with a space between each IP.
Explanation:
When a ConvertTo-Csv or Export-Csv is run, the input object property is converted using the ToString() method. If the reference type of that object property (System.Array in this case) does not have an override for the ToString() method, then that method will return the fully qualified type name of the property. In this instance, that FQTN is System.Object[]. This is predictable with a little digging.
Testing with [Int32], you would expect the string conversion to provide a string representation of the integer data because it does have an override:
$int = 1
$int.gettype().fullname
System.Int32
($int | Get-Member).where$_.Name -eq "ToString"
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provid...
$int.ToString()
1
$int.ToString().gettype().fullname
System.String
Testing with [Array], you would not expect the string conversion to provide a string representation of the array data because it does not have an override:
$arr = [array]1
$arr.gettype().fullname
System.Object[]
([System.Object[]] | Get-Member -Static).where$_.name -eq "ToString"
$arr.toString()
System.Object[]
See Export-Csv and Object.ToString Method for supplemental explanations and examples.
add a comment |
This happens because $vmachine.ipAddresses is a string array object. You want a string representation of that with controlled formatting. There are many ways to accomplish this. Here is one that will join multiple IPs (if they exist) using a ;. If there is only one IP, it will appear with no semi-colon:
$obj.ipAddresses = $vmachine.ipAddresses -join ";"
Here's an example of your scenario:
$ip = @("10.1.23.45")
$ip.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$obj.name = "test"
$obj.ip = $ip
$obj
name ip
---- --
test 10.1.23.45
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","System.Object[]"
Converting the ip property of $obj to string forces PowerShell to interpret the property as a string rather than a collection. Thus, the braces notation () goes away.
$obj.ip = $ip -join ";"
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","10.1.23.45"
Here are some other alternatives to set the ip property value as a string:
$obj.ip = -join $ip # No join character here. Works best with only one IP.
$obj.ip = $ip[0] # Accesses first element of array $ip, which will be a string. Only works with one IP.
$obj.ip = [string]$ip # Uses string type accelerator to cast $ip as string. This will join multiple IPs with a space between each IP.
Explanation:
When a ConvertTo-Csv or Export-Csv is run, the input object property is converted using the ToString() method. If the reference type of that object property (System.Array in this case) does not have an override for the ToString() method, then that method will return the fully qualified type name of the property. In this instance, that FQTN is System.Object[]. This is predictable with a little digging.
Testing with [Int32], you would expect the string conversion to provide a string representation of the integer data because it does have an override:
$int = 1
$int.gettype().fullname
System.Int32
($int | Get-Member).where$_.Name -eq "ToString"
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provid...
$int.ToString()
1
$int.ToString().gettype().fullname
System.String
Testing with [Array], you would not expect the string conversion to provide a string representation of the array data because it does not have an override:
$arr = [array]1
$arr.gettype().fullname
System.Object[]
([System.Object[]] | Get-Member -Static).where$_.name -eq "ToString"
$arr.toString()
System.Object[]
See Export-Csv and Object.ToString Method for supplemental explanations and examples.
add a comment |
This happens because $vmachine.ipAddresses is a string array object. You want a string representation of that with controlled formatting. There are many ways to accomplish this. Here is one that will join multiple IPs (if they exist) using a ;. If there is only one IP, it will appear with no semi-colon:
$obj.ipAddresses = $vmachine.ipAddresses -join ";"
Here's an example of your scenario:
$ip = @("10.1.23.45")
$ip.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$obj.name = "test"
$obj.ip = $ip
$obj
name ip
---- --
test 10.1.23.45
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","System.Object[]"
Converting the ip property of $obj to string forces PowerShell to interpret the property as a string rather than a collection. Thus, the braces notation () goes away.
$obj.ip = $ip -join ";"
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","10.1.23.45"
Here are some other alternatives to set the ip property value as a string:
$obj.ip = -join $ip # No join character here. Works best with only one IP.
$obj.ip = $ip[0] # Accesses first element of array $ip, which will be a string. Only works with one IP.
$obj.ip = [string]$ip # Uses string type accelerator to cast $ip as string. This will join multiple IPs with a space between each IP.
Explanation:
When a ConvertTo-Csv or Export-Csv is run, the input object property is converted using the ToString() method. If the reference type of that object property (System.Array in this case) does not have an override for the ToString() method, then that method will return the fully qualified type name of the property. In this instance, that FQTN is System.Object[]. This is predictable with a little digging.
Testing with [Int32], you would expect the string conversion to provide a string representation of the integer data because it does have an override:
$int = 1
$int.gettype().fullname
System.Int32
($int | Get-Member).where$_.Name -eq "ToString"
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provid...
$int.ToString()
1
$int.ToString().gettype().fullname
System.String
Testing with [Array], you would not expect the string conversion to provide a string representation of the array data because it does not have an override:
$arr = [array]1
$arr.gettype().fullname
System.Object[]
([System.Object[]] | Get-Member -Static).where$_.name -eq "ToString"
$arr.toString()
System.Object[]
See Export-Csv and Object.ToString Method for supplemental explanations and examples.
This happens because $vmachine.ipAddresses is a string array object. You want a string representation of that with controlled formatting. There are many ways to accomplish this. Here is one that will join multiple IPs (if they exist) using a ;. If there is only one IP, it will appear with no semi-colon:
$obj.ipAddresses = $vmachine.ipAddresses -join ";"
Here's an example of your scenario:
$ip = @("10.1.23.45")
$ip.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$obj.name = "test"
$obj.ip = $ip
$obj
name ip
---- --
test 10.1.23.45
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","System.Object[]"
Converting the ip property of $obj to string forces PowerShell to interpret the property as a string rather than a collection. Thus, the braces notation () goes away.
$obj.ip = $ip -join ";"
$obj | convertto-csv
#TYPE Selected.System.String
"name","ip"
"test","10.1.23.45"
Here are some other alternatives to set the ip property value as a string:
$obj.ip = -join $ip # No join character here. Works best with only one IP.
$obj.ip = $ip[0] # Accesses first element of array $ip, which will be a string. Only works with one IP.
$obj.ip = [string]$ip # Uses string type accelerator to cast $ip as string. This will join multiple IPs with a space between each IP.
Explanation:
When a ConvertTo-Csv or Export-Csv is run, the input object property is converted using the ToString() method. If the reference type of that object property (System.Array in this case) does not have an override for the ToString() method, then that method will return the fully qualified type name of the property. In this instance, that FQTN is System.Object[]. This is predictable with a little digging.
Testing with [Int32], you would expect the string conversion to provide a string representation of the integer data because it does have an override:
$int = 1
$int.gettype().fullname
System.Int32
($int | Get-Member).where$_.Name -eq "ToString"
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provid...
$int.ToString()
1
$int.ToString().gettype().fullname
System.String
Testing with [Array], you would not expect the string conversion to provide a string representation of the array data because it does not have an override:
$arr = [array]1
$arr.gettype().fullname
System.Object[]
([System.Object[]] | Get-Member -Static).where$_.name -eq "ToString"
$arr.toString()
System.Object[]
See Export-Csv and Object.ToString Method for supplemental explanations and examples.
edited Mar 23 at 13:47
answered Mar 23 at 12:34
AdminOfThingsAdminOfThings
2,9051214
2,9051214
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312981%2fhow-to-remove-curly-braces-and-get-proper-output-in-csv-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown