Note that there are some explanatory texts on larger screens.

plurals
  1. POInclude period in string if condition exists (inline)
    primarykey
    data
    text
    <p>I have variable $a in a foreach loop.</p> <p>I want to set a string for a filename.</p> <pre><code>$server = "mysqlservername" [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SmoEnum') | out-null $My='Microsoft.SqlServer.Management.Smo' $srv = new-object ("$My.Server") $Server $scripter = new-object ("$My.Scripter") $srv $scripter.Options.ToFileOnly = $true $a = new-object System.Data.Datatable $a=$srv.databases[$Database].EnumObjects([long]0x1FFFFFFF -band $all) $a | FOREACH-OBJECT { $scripter.Options.Filename = "$($_.schema).$($_.name)" #then script out an object using that filename) $UrnCollection = new-object ('Microsoft.SqlServer.Management.Smo.urnCollection') $URNCollection.add($_.urn) $scripter.script($URNCollection) } </code></pre> <p>I want the filename to be "schema.name", but only if $_.schema exists (not all objects have a schema). If it doesn't, I want it to be just "name" </p> <p>I'm not sure if, when it's empty, $a.schema is blank or null or empty. Not sure if that matters. </p> <p>The way it's set currently, if schema is empty(?), the filename is ".myobjectnamehere".</p> <p>Is there a way to do it in one line? I could do it with a ton of IF conditions, but I'd rather do it inline if I can. Thanks.</p> <p>Here's the contents of $a | gm</p> <pre><code> TypeName: System.Data.DataRow Name MemberType Definition ---- ---------- ---------- AcceptChanges Method void AcceptChanges() BeginEdit Method void BeginEdit() CancelEdit Method void CancelEdit() ClearErrors Method void ClearErrors() Delete Method void Delete() EndEdit Method void EndEdit() Equals Method bool Equals(System.Object obj) GetChildRows Method System.Data.DataRow[] GetChildRows(string relationName), System.Data.DataR... GetColumnError Method string GetColumnError(int columnIndex), string GetColumnError(string colum... GetColumnsInError Method System.Data.DataColumn[] GetColumnsInError() GetHashCode Method int GetHashCode() GetParentRow Method System.Data.DataRow GetParentRow(string relationName), System.Data.DataRow... GetParentRows Method System.Data.DataRow[] GetParentRows(string relationName), System.Data.Data... GetType Method type GetType() HasVersion Method bool HasVersion(System.Data.DataRowVersion version) IsNull Method bool IsNull(int columnIndex), bool IsNull(string columnName), bool IsNull(... RejectChanges Method void RejectChanges() SetAdded Method void SetAdded() SetColumnError Method void SetColumnError(int columnIndex, string error), void SetColumnError(st... SetModified Method void SetModified() SetParentRow Method void SetParentRow(System.Data.DataRow parentRow), void SetParentRow(System... ToString Method string ToString() Item ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string ... DatabaseObjectTypes Property string DatabaseObjectTypes {get;set;} Name Property string Name {get;set;} Schema Property string Schema {get;set;} Urn Property string Urn {get;set;} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload